Tuesday, October 4, 2011

our two ways of mocking methods

We have two ways of mocking with MOQ for the two different approaches to inversion of control. For Dependency Injection:

IQueryable<Foo> foos = TestObjects.BuildFooCollection().AsQueryable();

mockFooRepository = new Mock<IFooRepository>();

mockFooRepository.Setup(repo => repo.GetAll()).Returns(foos);

fakeFooRepo = mockFooRepository.Object;

 
 

In the situation above, fakeFooRepo would be handed to a FooRepository getsetter on a controller in lieu allowing dependency inject to put a real FooRepository there. Alternatively... For Service Locator:

registry = new MocksRegistry();

ServiceLocator.LookupSource = () =>

{

   return registry;

};

IQueryable<Bar> bars = TestObjects.BuildFooCollection().AsQueryable();

registry.BarRepositoryMock.Setup(repo => repo.GetAll()).Returns(bars);

No comments:

Post a Comment