Search code examples
c#nunitrhino-mocks

How to find out if method was called in a bool value in Rhino mocks?


I'm trying to write a parameterized unit test using NUnit and Rhino Mocks that can return true or false depending on whether a certain mocked method was called. AssertWasCalled is not right because it makes the test fail right away. I only want a bool value.

[Test]
[TestCase(1,2, Result=false)]
[TestCase(1,1, Result=true)]
public bool SomeTest(int a, int b)
{
    ...
    someObject.CheckValues(a, b); // logs something if values are different.

    return mockLogger.WasCalled(x => x.Log(null));
}

WasCalled ofc does't exist.


Solution

  • Stub the Log method on mockLogger to set a bool when it's called, and return that:

    bool logMethodWasCalled = false;
    mockLogger
        .Stub(x => x.Log(Arg<string>.Is.Equal(null))
        .Do(new Action<string>(_ => logMethodWasCalled = true));
    
    // Run test...
    
    return logMethodWasCalled;