Stubbing a generic method with different T

417 Views Asked by At

I have a Service Factory with a Generic method GetService. Based on what I pass for T it gets me a service for that particular class.

Now I have a method, which uses multiple of these services and I need to stub all of them. Ex: GetService<Coffee> , GetService<Tea>.

mockrepo.Stub(x => x.GetService<Coffee>().Expect(c => c.RetrieveList(coffeeCollection)).IgnoreArguments());
mockrepo.Stub(x => x.GetService<Tea>().Retrieve(1)).Return(testrefDataRefEle1);

In the above code, on the second stub I get error "Object reference not set to an instance of an object."

1

There are 1 best solutions below

0
On

You can't do a recursive mock like that in Rhino.Mocks. You'll have to create a stub for whatever object has the Retrieve method and set that up to return testrefDataRefEle1. Then set up the stub of GetService<Tea> to return the other mocked object.