My Rhino mocks Strict Mock expectation exceptions are being caught in the executing method. How to solve?

167 views Asked by At

I'm using Rhino Mocks to set up strict mocks. Under certain conditions, no method calls may be performed on any of these mocks.

// Arrange
var myMock = MockRepository.GenerateStrictMock<IMyClass>();
var sut = new SUT(myMock);

// Act
sut.DoSomething();

Now, DoSomething wraps everything in a try-catch:

public void DoSomething()
{
    try {
        m_Class.Something();
    }
    catch {
    }
}

This causes the expectationexception of the strict mock to be caught. My test passes while it shouldn't.

I would have hoped that calling myMock.VerifyAllExpectations(); would result in a failing test, but is not the case either.

How can I achieve this result?

1

There are 1 answers

1
John M. Wright On BEST ANSWER

Having a catch block that eats the exception is generally a bad practice. If you aren't able to change that, however, you can use do more explicit asserts on the members using .AssertWasCalled() and .AssertWasNotCalled(), like this:

myMock.AssertWasNotCalled(x => x.Something())

But that would require setting up an assertion for every possible interface member if you want to verify nothing was called, which would be tedious and prone to future members being missed.

Unfortunately, using Exceptions for assertions is a fundamental principal for RhinoMocks (and most unit testing frameworks, like NUnit, Moq, NSubstitute, etc), so catching those exceptions before they propagate to the testing framework will be a problem in general.