views:

428

answers:

2

Appreciate any good example or references to websites which has useful information of using TDD with Model-View-Presenter pattern using Rhino Mocks.

What I am looking for is with respect to the following points

  • What to mock (view and presenter)
  • The new AAA syntax
  • How to mock UI behaviour? e.g. if "firstName" and "lastName" entered in view enable the "Submit" button, else disable it. This is just a simple example.
  • Best practices

Any input in this direction is greatly appreciated.

Thanks in advance.

+3  A: 

Since you have not got an answer, I will do my best with what I have learned so far;

First question; what to mock - typically you would mock everything you are not testing. So given you are testing a ViewModel, you would mock view code that changes the ViewModel, along with mocking Model code that populates/persists the ViewModel.

Second question; AAA syntax - The AAA syntax is most easily kept by adding the following type of comments to your test methods;

[Test]
public void whenUserFillsInFirstAndLastName_ThenUserCanSubmit()
{
    // Arrange - code used to set-up what you are testing.
    this.loadViewModelWithInitalContext(viewModel); // This is a helper that loads the viewmodel

    // Act - code to fullfil the 'when' part in the test.
    this.viewModel.FirstName = "test";
    this.viewModel.LastName = "me";

    // Assert - code to check state of object being tested. (here I am testing a property that I bind to the enabled state of a submit type button)
    Assert.IsTrue(this.viewModel.UserCanSubmit);
}

Third question, mock UI behavior - typically you put that in the Act portion of the test (for testing ViewModel).

Last question, best practices, my experience says;

  • Assert only one thing at a time, then your tests will fail with a single failure, and you will save yourself a great deal of time when figuring out what went wrong. You could even go to having one test-method per assert, but ask a new question on how to do that, as it requires a different implementation of the AAA syntax to make things managable.
  • Use dependency injection, as it makes code easier to mock.
  • Make your tests as light weight as possible - do not go to database or disk you want your tests fast, so developers execute them as often as possible. If it takes me 5 minutes to run through the tests I get sick of waiting. If it takes me 10sec, I am willing to do it much more often.
  • Last - but most important - make sure you execute through all your unit tests on every single checkin by anyone on your team. This will keep your tests up to date, and also make your code base much more stable. Use contiues integration platforms like CruiseControl.net to build on a seperate server - as often as possible. The clue being, you want to know as quickly as possible if someone/thing has broken the build.

My recommended references; use 'your favorite search engine' to search for what you want to do, or post examples of your test here and get comments - this will help you improve what you are doing.

Thies
+1  A: 

Phil Haack has a pretty good blog post on this. Download the source code and take a look at the tests, they're commented pretty well.

Bob Nadler
I would not recommend this references for either AAA or writing unit tests with RhinoMocks; the code does not use the AAA syntax for tests, and the tests are written using Record/Reply which to me is a mess compared to the AAA syntax.
Thies