Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhino Mocks exhibits different behaviour in debug mode

Tags:

c#

rhino-mocks

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);
}
like image 991
keith Avatar asked Oct 19 '11 00:10

keith


2 Answers

Just had this exact problem. I deleted the hidden .suo file and sure enough it worked.

That's when i realized that by deleting the settings the exception catching options were also deleted. It was not stopping on any exception. Re-enabling the option in "Debug"->"Exceptions..." made the error re-appear.

It is however a First Chance exception so a simple F5 (Continue) will step over it and the code runs actually as expected.

So in short, the solution of deleting the suo file, means reset settings and thus ignore exception.

like image 76
Thanasis Kapelonis Avatar answered Oct 19 '22 06:10

Thanasis Kapelonis


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.

like image 37
keith Avatar answered Oct 19 '22 04:10

keith