Skip Navigation Links / Posts / Post
Search site. 
Powered by Google
Darren Neimke (Me)

My Book

Readify

">ASP.NET MVP


Interesting Portals 

NetVibes
This portal feels similar to PageFlakes in many ways but I love their gallery. They also have a feature whre certain chrome elements only become visible when you hover over the web part.

Xtra
A New Zealand news portal. I especially liked the content rotator web part at the top of the middle row. Seems like a nice way to allow a user to browse through data.

 

Posts Archive 

Using Rhino Mocks to test objects

Categories

Yesterday Phil Haack posted up this nice post about how to code the MVP pattern in a web application.  The implementation would look fairly familiar to anyone who has used MVC before but apparently big 'A' guy Martin Fowler asserts that there are now many different flavors of a model-view-controller|presenter pattern and in this post, Phil is showing us what the Supervising Controller pattern looks like.

One of the best things that I learned most from Phil's post was how he used the Rhino.Mocks library to create mock objects for his unit tests of his code.  This looks like a very simple way to test code that is normally difficult to test - such as ASP.NET code or code whose state is heavily reliant upon certain runtime conditions to be met.  In such cases you'll often find yourself factoring code out into interfaces and then writing testable stubs for them.  Mock objects save you from writing lots of additional code just for the sake of getting testable stubs of objects.  Here's some code from Phil's sample project in his post where he tests the events and the state of a view in his test code:

[TestMethod]
public void HandlesLoadEventAndSetsViewPropertiesCorrectly()
{
    //Setup the service return values.
 BlogPost blogPost = new BlogPost(1);
    blogPost.Tags.Add(new Tag("ASP.NET"));
 blogPost.Tags.Add(new Tag("C#"));
   
    blogPost.Description = "My computer's cup holder is broken.";
   
    //Setup the load event
    viewMock.Load += null; //ugly syntax, I know, but the only way to get this to work
    IEventRaiser loadRaiser = LastCall.IgnoreArguments().GetEventRaiser();

    // Make sure that the proper service methods were called
    // by the presenter.
    Expect.Call(this.dataServiceMock.GetById(1)).Return(blogPost);
    mocks.ReplayAll();

    //Now run the test.
    new PostEditController(viewMock, this.dataServiceMock);
    loadRaiser.Raise(viewMock, EventArgs.Empty);
   
    //Check that the view was properly populated.
    Assert.AreEqual(2, viewMock.Tags.Count);
    Assert.AreEqual("My computer's cup holder is broken.", viewMock.BlogPostBody);

    mocks.VerifyAll();
}

If the code looks a little odd then read from one of Phil's other fine article where he specifically shows how to use Rhino.Mocks to test events - that will help explain what some of that code is doing.

posted 8/11/2006 3:31:58 AM

 

Comments:

# Testing ASP.NET
posted by Haacked on 8/11/2006 5:48:52 AM :

Some aspects of ASP.NET are still hard to test with RhinoMocks because many classes are sealed. RhinoMocks can't mocked sealed classes.

That's why I still use this old technique.
http://haacked.com/archive/2005/06/11/4617.aspx

 

Comments are currently disabled for this post.