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



On Fri, Apr 23, 2010 at 4:24 PM, Steve <[email protected]> wrote:
> 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 [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rhinomocks?hl=en.
>
>

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

Reply via email to