views:

161

answers:

2

Hi Everyone,

I've been struggling with this all morning and I may be completely on the wrong track since Rhino Mocks was originally designed for Unit Testing.

However, I have a couple of resusable backend components that handle Authentication and 'Blogging' like features of a website, each contains their own data access layer. While developing the user interface, I really don't want to be messing around with creating test data in a database and / or an XML file.

What I'm striving for is an implementation of my data access interfaces (IBlogRepository), for example to run as singletons throughout the lifetime of a debugging session (aka an in-memory database) so that I can display some data to design against.

Hope that makes sense! All comments greatly appreciated.

Jason

+3  A: 

Don't use a mock, actually create a test implementation of your IBlogRepository interface, which returns hard-coded dummy data.

David M
Hi David, this was my backup plan from the start. It just would have been nice to save a bit of time by getting rhinomocks to create a load of dummy data for me! Thanks for taking the time to reply :)
Jason Summers
Take a look at NBuilder by the way - will help if you want to create a lot of data.
David M
+2  A: 

Singletons are at odds with Testability because they are so hard to change. You would be much better off using Dependency Injection to inject an IBlogRepository instance into your consuming classes:

public class MyClass
{
    private readonly IBlogRepository repository;

    public MyClass(IBlogRepository repository)
    {
        if(repository == null)
        {
            throw new ArgumentNullException("repository");
        }

        this.repository = repository;
    }

    // use this.repository in other members
}

Notice how the Guard Claus together with the readonly keyword guarantees that the IBlogRepository instance will always be available.

This will allow you to use Rhino Mocks or another dynamic mock library to inject Test Doubles of IBlogRepository into the consuming classes.

Mark Seemann
Hi Mark, I'm actually using structuremap for dependency injection in all my layers. I totally agree with what you've said. What I'm trying to do is dynamically create an implementation of said Interface at application runtime so that I can see some dummy data displayed on the page while I'm designing the UI.I think I'm more than likely going to go with David's response below though as it definately appears easier! Thanks for taking the time to reply :)
Jason Summers
Yes, what you are talking about then is a sort of Integration Testing where you want to replace the lowest layer with a Test Double. In such cases, a Fake as suggested by David M is the best option, although it may require significantly more effort - and then you may ask yourself why you don't use the real IBlogRepository implementation instead, and turn your Integration Test into a System Test?
Mark Seemann