Why I got this error message: Expected #1, Actual #0?

旧时模样 提交于 2021-01-28 20:00:51

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!