Getting DRY with Rhino Mocks

c#, dry, nunit, rhino-mocks, unit-testing

Solution

This may be controversial, but I favour readability, rather than DRY-ness* in unit tests.

In other words, set up methods are none existant in my unit tests. They are only used for integration tests. I believe XUnit.NET takes this stance too.

So to answer your question, I really would not worry about setting up mock presenters in each of your tests that require one. Some tests may not need a mock presenter, therefore having one set up before the test is run is not necessary.

**Naturally, my unit tests span say ten lines average, if this increases or the scope of setting up the test (following AAA - Arrange, Act Assert) is large, only then will I remove the duplication and create helper methods. To clear this point up, for cleaner tests, you can create a base test class that contains the helper methods and other set up code.*

Problem

I am looking for ways of making the following more concise. ``` public class MyTests { IPresenter presenter; [SetUp] public void SetUp() { presenter = MockRepository.GenerateStub<IPresenter>(); } ... } ``` In particular specifying the type again when creating the mock seems redundant. For example I can write it this way and use reflection to get the type and create the stub automatically: ``` public class MyTests { IPresenter presenter; [SetUp] public void SetUp() { Stub(x => x.presenter); } void Stub(Expression<Func<MyTests, object>> expression) { ... } } ``` This would work but the compiler can no longer detect that presenter is assigned and starts issuing warnings. This also makes ReSharper very unhappy. Can anyone suggest a better approach?

Original source