dimanche 28 février 2016

Unit Testing - Setting Model Property Values

I'm currently in the process of writing a unit test, which is failing... It states: failed. Expected:<0>. Actual:<16>. I understand that this occurs because the RateManagement model is not getting set properly, but unsure how to set this data in my test.

Question: How can I populate the RateManagement model so that I can test the values correctly?

Failing Test

[TestMethod]
public void BabysitterBusiness_StartTimeBedTimeAndEndTimeAreRecorded_ReturnsCorrectAmountDue()
{
    // Arrange
    var data = new RateManagement
    {
        TotalBedtimeHours = TimeSpan.FromHours(3),
        TotalHoursBeforeBedtime = TimeSpan.FromHours(2),
        TotalOvertimeHours = TimeSpan.FromHours(4)
    };

    // Act
    var result = _business.CalculatePaymentDue();

    // Assert
    Assert.AreEqual(result, 16M);
}

RateManagement Class

public class RateManagement 
    {
        public TimeSpan TotalBedtimeHours { get; set; }
        public TimeSpan TotalOvertimeHours { get; set; }
        public TimeSpan TotalHoursBeforeBedtime { get; set; }
    }

Business Class

using System;
using System.Globalization;
using BabysitterKata.Models;

namespace BabysitterKata.Business
{
    public class BabysitterBusiness
    {
        private readonly TimeManagement _timeManagement;
        private readonly RateManagement _rateManagement;
        private readonly HourlyRate _hourlyRate = new HourlyRate
        {
            StartingRate = 12M,
            BedtimeRate = 8M,
            OvertimeRate = 16M
        };

        public BabysitterBusiness(TimeManagement timeManagement, RateManagement rateManagement)
        {
            _timeManagement = timeManagement;
            _rateManagement = rateManagement;
        }

        public decimal CalculatePaymentDue()
        {
            // calculate amount due for total hours
            var beforeBedtimeAmountDue = _hourlyRate.StartingRate * _rateManagement.TotalHoursBeforeBedtime.Hours;
            var duringBedtimeAmountDue = _hourlyRate.BedtimeRate * _rateManagement.TotalBedtimeHours.Hours;
            var afterHoursAmountDue = _hourlyRate.OvertimeRate * _rateManagement.TotalOvertimeHours.Hours;

            // rounding up if not at start of hour
            if (_rateManagement.TotalHoursBeforeBedtime.Minutes != 00)
            {
                beforeBedtimeAmountDue += _hourlyRate.StartingRate;
            }

            if (_rateManagement.TotalBedtimeHours.Minutes != 00)
            {
                duringBedtimeAmountDue += _hourlyRate.BedtimeRate;
            }

            if (_rateManagement.TotalOvertimeHours.Minutes != 00)
            {
                afterHoursAmountDue += _hourlyRate.OvertimeRate;
            }

            return beforeBedtimeAmountDue + duringBedtimeAmountDue + afterHoursAmountDue;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire