This is a test suite that is green using Rhino Mocks.
[SetUp]
public void BeforeEachTest()
{
_mocksRepo = new MockRepository();
_mockBank = _mocksRepo.StrictMock<IBank>();
//_mockPrinter = _mocksRepo.StrictMock<IPrinter>();
_mockPrinter = _mocksRepo.DynamicMock<IPrinter>();
_mockLogger = _mocksRepo.StrictMock<ILog>();
_testSubject = new CrashTestDummy(DUMMY_NAME, _mockPrinter, _mockLogger);
}
[TearDown]
public void AfterEachTest()
{
_mocksRepo.ReplayAll(); // 2nd call to ReplayAll does nothing. Safeguard check
_mocksRepo.VerifyAll();
}
[Test]
public void Test_ConstrainingArguments()
{
_mockPrinter.Print(null);
LastCall.Constraints(Text.StartsWith("The current date is : "));
_mocksRepo.ReplayAll();
_testSubject.PrintDate();
}
Now to make a test green in another fixture, I had to make a slight change to the ctor - subscribe to an event in the printer interface. This resulted in all tests in the above test fixture going red.
public CrashTestDummy(string name, IPrinter namePrinter, ILog logger)
{
_printer = namePrinter;
_name = name;
_logger = logger;
_printer.Torpedoed += KaboomLogger; // CHANGE
}
The NUnit errors tab shows
LearnRhinoMocks.Rhino101.Test_ConstrainingArguments:
TearDown : System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> Rhino.Mocks.Exceptions.ExpectationViolationException : IPrinter.add_Torpedoed(System.EventHandler`1[LearnRhinoMocks.MyEventArgs]); Expected #1, Actual #0.
The way to fix this is to move the line where the test subject is created from Setup()
below the ReplayAll()
line in the test. Rhino mocks thinks that you have setup an event subscribe as an expectation otherwise. However this fix means (some) duplication in each test.
Each test usually adds some expectations before calling ReplayAll.
I know that this is a specific scenario which involves event subscription in the test subject ctor.
- However this is a normal scenario e.g. in a ModelViewPresenter pattern, I'm curious to know if there is a recommended way to do this?
- Also I didnt like the way multiple tests in a test fixture failed due to change driven by an external test ? Am I in test-design smell country?