[英]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>.
我目前正在编写单元测试,但失败了……它指出:
failed. Expected:<0>. Actual:<16>.
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. 我知道发生这种情况是因为未正确设置
RateManagement
模型,但不确定如何在测试中设置此数据。
Question: How can I populate the
RateManagement
model so that I can test the values correctly?问题:如何填充
RateManagement
模型,以便可以正确测试值?
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;
}
}
}
You are on the right track. 您走在正确的轨道上。 You are constructing the data instance.
您正在构造数据实例。
Just pass that and 2 more similar instances to the constructor of _business. 只需将它和另外2个相似的实例传递给_business的构造函数即可。
// Arrange
var data = new RateManagement
{
TotalBedtimeHours = TimeSpan.FromHours(3),
TotalHoursBeforeBedtime = TimeSpan.FromHours(2),
TotalOvertimeHours = TimeSpan.FromHours(4)
};
var timeData = new TimeManagement { // your properties };
// Act
var business = new BabysitterBusiness(timeData, data);
var result = business.CalculatePaymentDue();
// Assert
Assert.AreEqual(result, 16M);
Your RateManagement and TimeManagement seem to be simple data objects, so there is no need to Mock it using a framework or anything. 您的RateManagement和TimeManagement似乎是简单的数据对象,因此无需使用框架或其他任何东西来模拟它。 You can pass in the real instance with data.
您可以将真实实例与数据一起传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.