【问题标题】:Rhino.Mocks.Exceptions.ExpectationViolationException : Expected #1, Actual #2Rhino.Mocks.Exceptions.ExpectationViolationException : 预期 #1,实际 #2
【发布时间】:2014-04-25 14:20:49
【问题描述】:

我有两个测试。第一次测试工作正常。第二次测试看起来像第一次测试。但是,当我运行第二个测试时,我得到了异常。

第一次测试:

public void GetCurrentBalance_Should_getting_rigth_balanse()
{
    // Arrange
    double balance = 10;

    var mocks = new MockRepository();
    IUnitOfWork unitOfWork = mocks.Stub<IUnitOfWork>();
    unitOfWork.Stub(svc => svc.AccountRepository.Get()).Return(new List<Account> {new Account { Balance = balance }}); 

    AccountService accountService = new AccountService(unitOfWork); 

    // Act
    mocks.ReplayAll();
    var result = accountService.GetCurrentBalance();

    //Assert
    Assert.AreEqual(balance, result);
}

第二次测试:

public void WithdrawMoney_Balance_should_decrease_when_money_been_withdrawn()
{
    // Arrange
    double balance = 10;

    var mocks = new MockRepository();
    IUnitOfWork unitOfWork = mocks.Stub<IUnitOfWork>();
    unitOfWork.Stub(svc => svc.AccountRepository.Get()).Return(new List<Account> { new Account { Balance = balance } });

    AccountService accountService = new AccountService(unitOfWork);

    // Act
    mocks.ReplayAll();
    var result = accountService.WithdrawMoney(1); // this line is different

    //Assert
    Assert.AreEqual(balance - 1, result);
}

我的部分服务:

public class AccountService : BaseService, IAccountService
{
    public AccountService(IUnitOfWork unitOfWork)
        : base(unitOfWork)
    {
    }

    public double WithdrawMoney(double amountOfMoney)
    {
        var currentBalance = GetCurrentBalance();

        if (currentBalance < amountOfMoney)
        {
            throw new BusinessLayerException("error!");
        }

        var lastAccount = GetLastAccount();

        if (lastAccount == null)
        {
            throw new BusinessLayerException("error!");
        }

        lastAccount.Balance -= amountOfMoney;

        unitOfWork.AccountRepository.Update(lastAccount);
        unitOfWork.Save();

        return lastAccount.Balance;
    }

    public double GetCurrentBalance()
    {
        var lastAccount = GetLastAccount();

        if (lastAccount == null)
        {
            return 0;
        }

        return lastAccount.Balance;
    }

    private Account GetLastAccount()
    {
        var lastAccount = unitOfWork.AccountRepository.Get().FirstOrDefault();

        return lastAccount;
    }
}

我得到以下调用堆栈:

Rhino.Mocks.Exceptions.ExpectationViolationException : IUnitOfWork.get_AccountRepository(); Expected #1, Actual #2.
   at Rhino.Mocks.MethodRecorders.UnorderedMethodRecorder.DoGetRecordedExpectation(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
   at Rhino.Mocks.MethodRecorders.MethodRecorderBase.GetRecordedExpectation(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
   at Rhino.Mocks.Impl.ReplayMockState.DoMethodCall(IInvocation invocation, MethodInfo method, Object[] args)
   at Rhino.Mocks.Impl.ReplayMockState.MethodCall(IInvocation invocation, MethodInfo method, Object[] args)
   at Rhino.Mocks.MockRepository.MethodCall(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
   at Rhino.Mocks.Impl.Invocation.Actions.RegularInvocation.PerformAgainst(IInvocation invocation)
   at Rhino.Mocks.Impl.RhinoInterceptor.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.IUnitOfWorkProxy2936ffa6dea844258ad88f7a7e99dbc0.IUnitOfWork.get_AccountRepository()
   at Service.AccountService.GetLastAccount() in AccountService.cs: line 90
   at Service.AccountService.WithdrawMoney(Double amountOfMoney) in AccountService.cs: line 61
   at ServiceTest.AccountServiceTest.WithdrawMoney_Balance_should_decrease_when_money_been_withdrawn() in AccountServiceTest.cs: line 48

【问题讨论】:

    标签: c# unit-testing nunit rhino-mocks


    【解决方案1】:

    遵循以下 Rhione 模拟测试规则

    要使用 Rhino Mocks,只需按照以下步骤使用 Mock Object:

    1.创建模拟:mockrepository.CreateMock();

    2.记录你的表达:在记录块中。

    3.调用 ReplayAll 告诉 rhino Mocks 你已经完成了录制

    4.MOST IMP。 STEP:在这里调用期望的方法例如:mockMyRhinoImplementation.HelloRhinoMocks(“ABC”)

    注意:必须传递您记录的相同参数,否则它将再次抛出 ExpectationViolationException。

    5.调用VerifyAll();

    所以,这两种测试的区别在于一种方法有参数,而另一种方法没有...尝试像下面这样使用

    double number = 1;
    var result = accountService.WithdrawMoney(number);
    

    【讨论】:

      【解决方案2】:

      我通过更改第二个测试来解决我的问题。我改变了一行: 来自:

      IUnitOfWork unitOfWork = mocks.Stub<IUnitOfWork>();
      

      到:

      IUnitOfWork unitOfWork = mocks.DynamicMock<IUnitOfWork>();
      

      但在我看来,这对我的问题来说是一个糟糕的解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多