Search code examples
rhino-mocksassert

How to assert if a method was called within another method in RhinoMocks?


I have a class that has two methods. One method needs to call the other method and in my test I want to assert that it was called.

public class Tasks : ITasks
{
  public void MethodOne()
  {
    MethodTwo(1);
  }

  public int MethodTwo(int i)
  {
    return i + 1;
  }
}

I want to mock Tasks and do something like tasks.AssertWasCalled(x => x.MethodTwo(1)). Must MethodTwo be virtual?


Solution

  • Of course my thinking at that time was flawed. I should be mocking ITasks, not the implementation (Tasks):

    ITasks tasks = MockRepository.GenerateMock<ITasks>();
    tasks.AssertWasCalled(x => x.MethodTwo(Arg<int>.Is.Equal(1)));