问题
Why the following test code using Rhino Mocks raises this exception: Expected #1, Actual #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.
}
回答1:
Probably because expecting returning of cart object.
Maybe you may use Stub instead of Expect and verify using 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());
}
来源:https://stackoverflow.com/questions/23507684/why-i-got-this-error-message-expected-1-actual-0