我正在尝试编写一个计算订单总数的单元测试。但是,我正在测试的CalculateOrder()方法中调用了另一种方法,并且测试一直失败,提示“对象引用未设置为对象的实例。”
这是我正在测试的CalculateOrder方法...
public App_OrderForm CalculateOrder(App_OrderForm form)
{
try
{
// Get the possible fees for the form group
short? orderGroupId = form.Ref_OrderType == null
? GetOrderGroup(form.OrderTypeId)
: form.Ref_OrderType.orderGroupId;
Dictionary<Ref_OrderType, List<short>> fees =
GetFeeTypeForOrderGroup((OrderGroup)orderGroupId);
这是导致错误的最后一行代码。
这是我的单元测试...
// Arrange
_mockUnitOfWork = new Mock<IUnitOfWork>();
_feeCalculator = new FeeCalculator(_mockUnitOfWork.Object);
App_OrderForm form = GenerateMockedForm(OrderFormType.Wholesaler);
form.Ref_OrderFormType = new Ref_OrderFormType
{
OrderFormTypeId = 1,
OrderGroupId = 1
};
foreach (var item in form.App_OrderFormItem)
{
// add calculated fees
item.App_OrderFormItemCalculatedFee = new List<App_OrderFormItemCalculatedFee>
{
new App_OrderFormItemCalculatedFee()
{
OrderFormItemCalculatedFeeId = 1,
FeeTypeId = 1,
Amount = 0.044M,
NumberOfFees = 1
},
};
}
// Act
Mock<IReferenceDataService> rds = new Mock<IReferenceDataService>();
rds.Setup(x => x.GetAllPayableStatus())
.Returns(new List<Ref_Status> {new Ref_Status() {StatusId = (short) Status.ReadyForPayment}});
_feeCalculator.CalculateForm(form, false);
我应该设置GetFeeTypeForOrderGroup()方法吗?在单元测试开始时,我已经设置了一个订购单,并为OrderGroupId赋予了一个值,那么为什么GetFeeTypeForOrderGroup()方法不能运行或为什么返回错误?
希望如此
谢谢