Search code examples
c#.netunit-testingrhino-mocks

How can I pass a unit test immediately after an expected method is called on the mock object?


When working with Rhino.Mocks with a mock object in hand:

Is there a way to pass a unit-test once an expected method is called without executing the lines after the call to this expected method?

Thanks


Solution

  • Since RhinoMocks 3.5 you can use nice AssertWasCalled()

    this.Service.BuldMessage("messageId");
    this.Service.AssertWasCalled(x => x.GenerateMessage("messageId"), messageId));
    

    EDIT: Answer to comment

    RhinoMock is not in charge to change test execution flow, so you have to use NUnit asserts, Assert.Pass() utility method allows you to immediately end the test, recording it as successful:

    if (this.Service.AssertWasCalled(...)))
    {
       Assert.Pass("well done");
    }
    

    PS: as others suggested consider redesing of unit test which forced you to do such conditional test exit.