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);
}