[RhinoMocks] Noob Question about Mocking a Repository from an Interface

8 views
Skip to first unread message

Steve

unread,
Apr 23, 2010, 4:24:24 PM4/23/10
to Rhino.Mocks
I'm using NHibernate for data access and for each table I have a
repository and an interface:

// Here is the repository interface
Public Interface IFooRepository
Foo GetFooById(int ID)
Foo GetFooByName(string name)

// Here is the NHibernate implementation of the repository Interface
Public Class NHibernateFooRepository : IFooRepository
...


So far so good. I'm using Test Driven Development and what I'd like to
do in my unit tests is mock out my IFooRepository so that I can mock
out my data access layer when testing business logic (I have seperate
classes for business logic and data access). I could of course create
a MockRepository class and add it to my unit test project:

Public Class MockFooRepository : IFooRepository
public Foo GetFooByID(int ID).....

This works just fine as the GetoFoo.. methods on my MockFooRepository
return exactly what I tell them to. However I have lots of tests and I
want to return different stuff on each test which means I end up with
MockFoo1, MockFoo2, MockFoo3... to cover all the unit test scenarios.
Ideally I'd like to just generate a mock object using Rhino Mocks
based on my interface: IFooRepository. My question is how can I create
a mock object instance based my interface _and_ control what get's
returned from my GetFooById() and GetFooByName() methods. I probably
want these to return something different in each test case. Can I
somehow use a lambda expression or delegate on my unit test class so
that I can wire up custom code for each test case?

Apologies if this obvious or I'm taking totally the wrong approach but
I'm new to Rhino Mocks and the world of Mocking!

--
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To post to this group, send email to rhino...@googlegroups.com.
To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rhinomocks?hl=en.

Patrick Steele

unread,
Apr 23, 2010, 5:25:50 PM4/23/10
to rhino...@googlegroups.com
This is easy with Rhino.Mocks:

var repository = MockRepository.GenerateStub<IFooRepository>();
var foo1 = new Foo { prop1 = "1", prop2 = 2.0, etc... };
var foo2 = new Foo { prop1 = "2", prop2 = 3.3, etc...};
repository.Stub(r => r.GetFoo(1)).Return(foo1);
repository.Stub(r => r.GetFoo(2)).Return(foo2);

Now just pass 'repository' and any call to "GetFoo" with a value of 1
will return foo1. Likewise, a call with value of 2 will return foo2.

---
Patrick Steele
http://weblogs.asp.net/psteele

Steven Camsell

unread,
Apr 23, 2010, 5:43:49 PM4/23/10
to rhino...@googlegroups.com
Perfect, just what I needed. Will give this a try.

Thanks very much Patrick, your advice is appreciated.

Steve Camsell

On 23 Apr 2010, at 22:25, Patrick Steele <patrick...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages