var blog = new ThoughtStream(me); RSS 2.0
 Tuesday, July 15, 2008

In the last month or two, I have been hand coding a lot of mock and stub objects and it has become a nightmare to manage. My primary reason for doing this by hand was that Rhino Mocks 3.4 and older did not fit with the BDD style unit tests that I was writing. Yes, I made it work in a few places, but it was ugly and annoying.

Fortunately, Ayende has cleaned it all up with the new syntax and made it very easy to assert that individual expectations were met, with v3.5. I finally got around to trying out the new syntax today, and I immediately fell in love with it. For example:

[Concern("User Administration")]
public class When_accessing_the_system_as_a_non_administrator : ContextSpecification
{
    private IMainView view;
 
    private IMainView GetView()
    {
        IMainView mockView = MockRepository.GenerateMock<IMainView>();
        mockView.Expect(v => v.EnableUserManagement()).Repeat.Never();
        mockView.Expect(v => v.DisableUserManagement()).Repeat.Once();
        return mockView;
    }
 
    protected override void Context()
    {
        User administrator = new User();
        administrator.IsAdministrator = false;
        CurrentUser.User = administrator;
 
        view = GetView();
        new MainPresenter(view);
    }
 
    [Observation]
    public void Should_not_display_the_User_Management_option()
    {
        view.AssertWasNotCalled(v => v.EnableUserManagement());
    }
 
    [Observation]
    public void Should_hide_the_user_management_option()
    {
        view.AssertWasCalled(v => v.DisableUserManagement());
    }
 
}

In the "GetView" method, I am setting up two very distinct expectations on my mock object.

  1. Never Call the EnableUserManagement method.
  2. Call the DisableUserManagement method once.

With the new Rhino Mocks syntax, I can easily verify that each one of these expectations was called via my "should" observations.

The "AssertWasNotCalled" extension method verifies that an expectation of Repeat.Never was setup and that the method was not called.

mockView.AssertWasNotCalled(v => v.EnableUserManagement());

And the "AssertWasCalled" extension method verifies that the expectation was to call the method, and that the method actually was called.

mockView.AssertWasCalled(v => v.DisableUserManagement());

I like it. The new syntax has really simplified my Context Specification testing.

Tuesday, July 15, 2008 2:43:10 PM (Central Standard Time, UTC-06:00)  #    Comments [1]. Trackback 
Tags: .NET | Behavior Driven Development | Lambda Expressions | Rhino Mocks | Unit Testing | User Stories

Tuesday, July 15, 2008 6:10:24 PM (Central Standard Time, UTC-06:00)
It's actually even simpler than that: you do not need to declare the expectations in your GetView() method (mockView.Expect...). Just create the mock and then assert your expectations at the end (in your Observations). The call to AssertWasCalled sets and verifies the expectations for you.
Comments are closed.
Navigation
About Me
View Derick Bailey's profile on LinkedIn

Send mail to the author(s) Contact Me
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Derick Bailey
Sign In
Statistics
Total Posts: 115
This Year: 0
This Month: 0
This Week: 0
Comments: 44
Themes
Pick a theme:
All Content © 2012, Derick Bailey
DasBlog theme 'Business' created by Christoph De Baene (delarou)