There are different ways to create and use stubs, you can use a repository to create stubs and handle all together, or use .Net Generics to create an independent stub.
Creating a MockRepository
[Test] public void CanSendMessageWithoutTextButWithAttachment2() { var mockRepository = new MockRepository(); var myStub = mockRepository.Stub<IMyClassType>(); // Creating a stub: myStub.Stub(x => x.MyMethod(null)).IgnoreArguments().Return(0); // Also you can create an expectation if you are expecting this method to be called: myStub.Expect(x => x.MyMethod(null)).IgnoreArguments().Return(0); mockRepository.ReplayAll(); // THIS IS VERY IMPORTANT!! var service = new MyService(myStub); // injecting the dependency var response = service.PerformRequest(); Assert.AreEqual(0, response); mockRepository.VerifyAll(); // THIS IS IMPORTANT TOO!! }
In this way of working we create a repository for mocking that works as a workarea, we can create stubs on it and verify all of them at the end of the test, but to make sure that the stubs will work we need to call “ReplayAll”. The reason for this is because we could add more stubs later on on the code flow and we need to tell the repository to reload and prepare them for use.
With this approach it looks quite useful to declare the repository on starting the test fixture and verify on finishing each test like this:
[SetUp] public void Init() { mockRepository = new MockRepository(); } [TearDown] public void TearDown() { mockRepository.VerifyAll(); }
Also, sometimes when you are testing a unit you may want to prepare some stubs and their returns to the calls of their methods, you could do that on the Init to save time and resources apart of generating simpler and cleaner codes. Be warn of doing the same to set up the Expectations though, as not all the tests will have the same expectations, which is why the call to “ReplayAll” is needed, to tell the test when are the stubs ready to be loaded and prepared.
Creating an independent stub
But if what you are doing is a stub that is used only by one test you may not be interested on using the mockRepository. If that is the case, you may prefer to use this more independent approach:
[Test] public void CanSendMessageWithoutTextButWithAttachment() { var myStub = MockRepository.GenerateStub<IMyClassType>(); myStub.Stub(x => x.MyMethod(null)).IgnoreArguments().Return(0); // Change for Expect if you want to set an expectation. var service = new MyService(myStub); // injecting the dependency var response = service.PerformRequest(); Assert.AreEqual(0, response); myStub.VerifyAllExpectations(); // This is important if there are expectations!! }
This time, instead of using a mockRepository we just created an isolated Stub to handle a dependency. We didn’t set any Expectations (though that is as easy as setting Expect instead than Stub) but if we had we would need that “VerifyAllExpectations” in the end, which will only check the expectations for this Stub, if you have more stubs with expectations you will need to call it once for each of them or use a mockRepository to simplify.
With this approach though, we don’t need to set the “ReplayAll” to make it work, it just will. You could also prepare the stubs at the Init and the tests will just work, if you need to set up an expectation you can do that inside each individual test and verify the stub expectations at the end of that test (or add a verify at TearDown, as if there aren’t expectations that shouldn’t crash anything and you avoid some developer forgetting to check them).
Warning
Do not mix these two ways of creating stubs or you will end up with a mess difficult to handle.