Search code examples
c#rhino-mocks

Rhino Mocks exhibits different behaviour in debug mode


I have an issue where a simple Rhino Mock stub method will work perfectly fine when I run a unit test, but will throw the exception Can't create mocks of sealed classes when executed in debug mode. I've tried replacing the Do with a Return method, but that hasn't changed the behaviour.

Using C# with Rhino Mocks 3.6, apologies for offending anyone by making an Add function subtract in a unit test ;)

Interface

public interface ICalculator
{
    int Add(int value, int value2);
}

Classes

public class Calculator : ICalculator
{
    public int Add(int value, int value2)
    {
        return value + value2;
    }
}

public class Sums
{
    private ICalculator calculator;

    public Sums(ICalculator calculatorArg)
    {
        calculator = calculatorArg;
    }

    public int Add(int value, int value2)
    {
        return calculator.Add(value, value2);
    }
}

Unit Test

[TestMethod()]
public void AddTest()
{
    //ARRANGE
    var calculatorArg = MockRepository.GenerateMock<ICalculator>();

    Func<int, int, int> subtract = delegate(int valueArg, int value2Arg)
    {
        return valueArg - value2Arg;
    };
    calculatorArg.Stub(x => x.Add(-1,-1)).IgnoreArguments().Do(subtract);

    Sums target = new Sums(calculatorArg);

    int value = 5;
    int value2 = 3;
    int expected = 2;

    //ACT
    int actual = target.Add(value, value2);

    //ASSERT
    Assert.AreEqual(expected, actual);
}

Solution

  • Delete the suo file

    Explanation: After PatrickSteele kindly pointed out to me that creating a fresh project with my code does actually work I compared every file in the original and new projects and found that only the suo files were different. (other than Guids, project names etc).

    After deleting the .suo file for the solution the problem was solved. Not my favourite answer to this question, but an answer none-the-less.