Reputation: 3707
I have two tests. First test work correctly. Second test looks like as first test. However, I get exception, when I run the second test.
The first test:
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);
}
The second test:
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);
}
Part of my service:
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;
}
}
I get the following call stack:
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
Upvotes: 4
Views: 8639
Reputation: 3707
I fix my problem by changing the second test. I change one line: from:
IUnitOfWork unitOfWork = mocks.Stub<IUnitOfWork>();
to:
IUnitOfWork unitOfWork = mocks.DynamicMock<IUnitOfWork>();
But it seems to me that this is a bad solution to my problem.
Upvotes: 1
Reputation: 11741
follow below rules for Rhione mock test
for using Rhino Mocks just follow the steps as under with Mock Object:
1.Create mock by: mockrepository.CreateMock();
2.Record your expactations: in Record Block.
3.call ReplayAll to tell rhino Mocks that you are done with recording
4.MOST IMP. STEP:Call expected method here for example: mockMyRhinoImplementation.HelloRhinoMocks(“ABC”)
NOTE: Must need to pass the same arguments what you have recorded else it will again throw ExpectationViolationException.
5.Call VerifyAll();
So, the differene between both the tests are tht one method is having argument and the another does not have...try to use like below
double number = 1;
var result = accountService.WithdrawMoney(number);
Upvotes: 3