Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Moq.Linq.MockRepository do?

Tags:

c#

mocking

moq

I am trying to figure out what does the MockRepository do.
I see that there is a Create method which I understand and it also has Of and OneOf methods which I don't understand.
What should T be? Mock or T itself?
How should the expression look like? Is it the setup expression?
What exactly am I querying here? Is it just another way to create a mock or does it track existing mocks and fetches them?

like image 918
the_drow Avatar asked Apr 21 '11 03:04

the_drow


2 Answers

I think it is used to allow easier verification of multiple mocks (from the moq docs -sort of):

        var mockRepo = new Moq.MockRepository(MockBehavior.Default);
        var foo = mockRepo.Create<IFoo>();
        var bar = mockRepo.Create<IBar>();

set up mocks and excercise the mocks

        mockRepo.VerifyAll();

in any case, what are you trying to accomplish with this class?

like image 144
Jaime Avatar answered Nov 14 '22 16:11

Jaime


I'm having trouble with MockRepository, too. Perhaps I have a conceptual misunderstanding of the class. My view of MockRepository is that it creates something akin to template instances of a mock.

To use the example interfaces, suppose IFoo had several properties and methods on it. I'd like to setup a template mock instance at the test fixture level that has some reasonable default return value for each property and method. Then, in each test method, override those values with values appropriate for the test at hand. This saves having to setup each IFoo mock in every test.

In a previous project, I wrote my own mock factory class that did this, but the description of MockRepository sounds like the same thing.

Am I misunderstanding MockRepository?

like image 36
Craig Boland Avatar answered Nov 14 '22 18:11

Craig Boland