我想模拟一个方法,但我的方法是在条件下,我需要的值是真的,这个操作使我的测试通过,如果我不模拟我的方法,它给我空异常,所以这很重要
[Test]
public void Can_return_price_according_to_semester_status()
{
var product = new Product
{
ProductTypeId = 15,
Id = 1,
Name = "Product name 1",
Price = 12.34M,
CustomerEntersPrice = false,
Published = true,
PreRegistrationPrice = 10.99M
};
_productService.Setup(x => x.GetSemesterProductTypeIds(It.IsAny<int[]>())
.Contains(product.ProductTypeId)).Returns(true);// this is my target mock
var customer = new Customer();
_priceCalcService.GetFinalPrice(product, customer, 0, false, 1).ShouldEqual(product.Price);
_priceCalcService.GetFinalPrice(product, customer, 0, false, 1).ShouldEqual(product.PreRegistrationPrice);
}
和GetFinalPrice:
if (_productService.GetSemesterProductTypeIds().Contains(product.ProductTypeId))// this is target of my mock
{
if (enrollmentTypeId <= 0)
{
if (product.SemesterStatus == SemesterStatus.PreRegistration)
{
enrollmentTypeId = (int)EnrollmentType.PreRegistered;
}
}
if (enrollmentTypeId == (int)EnrollmentType.PreRegistered)
{
price = product.PreRegistrationPrice;
}
}// some operation after this
problem