为什么我得到这个错误信息:期望#1,实际#0
本文关键字:期望 实际 信息 错误 为什么 | 更新日期: 2023-09-27 18:02:16
为什么下面使用Rhino mock的测试代码会引发这个异常:预期#1,实际#0 ?
int PRODUCT_ID = 1;
[TestMethod]
public void If_Cart_Already_Exists_Then_AddToBasket_Should_Use_It_ByCallingIShoppingCartGetCart()
{
ShoppingCart cart = new ShoppingCart(); //without UserName as ctor parameter
IShoppingCartRepository shoppingCartRepository = MockRepository.GenerateMock<IShoppingCartRepository>();
shoppingCartRepository.Expect(r => r.GetCart()).Return(cart);
ShoppingCartController c = new ShoppingCartController(shoppingCartRepository); //DI
c.AddToCart(PRODUCT_ID);//interAct
shoppingCartRepository.VerifyAllExpectations(); //Error: IShoppingCartRepository.GetCart(); Expected #1, Actual #0.
}
可能是因为期待购物车对象返回。
也许你可以使用Stub代替Expect,并使用AssertWasCalled进行验证。
public void If_Cart_Already_Exists_Then_AddToBasket_Should_Use_It_ByCallingIShoppingCartGetCart()
{
ShoppingCart cart = new ShoppingCart(); //without UserName as ctor parameter
IShoppingCartRepository shoppingCartRepository = MockRepository.GenerateMock<IShoppingCartRepository>();
shoppingCartRepository.Stub(r => r.GetCart()).Return(cart);
ShoppingCartController c = new ShoppingCartController(shoppingCartRepository); //DI
c.AddToCart(PRODUCT_ID);//interAct
shoppingCartRepository.AssertWasCalled(r => r.GetCart());
}