为什么使用Rhino Mocks的以下测试代码会引发此异常:预期为#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.
}
答案 0 :(得分:3)
可能是因为期待购物车对象的回归。
也许您可以使用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());
}