tags:

views:

147

answers:

5

Currently I'm reading a book (Pro ASP.Net Framework).

In this book, the author suggests to use a Moq framework to help to do TDD.

[Test]
public void List_Presents_Correct_Page_Of_Products()
{
    IProductsRepository repository = MockProductsRepository(
        new Product { Name = "P1" }, new Product { Name = "P2" },
        new Product { Name = "P3" }, new Product { Name = "P4" },
        new Product { Name = "P5" }
    );

    ProductsController controller = new ProductsController(repository);
    ...
}


static IProductsRepository MockProductsRepository(params Product[] prods)
{
    // Generate an implementor of IProductsRepository at runtime using Moq
    var mockProductsRepos = new Moq.Mock<IProductsRepository>();
    mockProductsRepos.Setup(x => x.Products).Returns(prods.AsQueryable());
    return mockProductsRepos.Object;
}

In the model layer, we've define a FakeRepository, and a SqlRepository.

The fact is I don't see the advantage of using this moq framework. Why don't we only use our FakeRepository ? Or clear our FakeRepository and add fake product on it ?

At first, I thought that the moq framework was there to generate fake data so you don't have to if you have for example 100 fake objects to generate.

What I miss ?

+5  A: 

Some advantages of mock frameworks over hand-rolled mocks:

  • The mock behavior is closer to the test code because it is in the test code. This makes tests easier to understand without having to look at other code.
  • You don't need to create yet another mock class (or worse: add logic to an existing one) just because you want slightly different behavior.
  • You end up writing less boilerplate because you only need to setup the methods you want to mock, while with hand-rolled ones you have to implement the whole interface.

Some disadvantages:

  • You have to learn a framework, while anyone can write mocks by hand.
  • It is yet another dependency you add to your project.

(This is what I can think of right now. Feel free to edit and add more.)

Martinho Fernandes
The Mock repository can be used to generate exceptions as well, rather than tweak the fake repository into obscurity.
Frank Schwieterman
A: 

We use it for essentially making a fake server in a client/server environment.

That way, calls don't have to go all the way from the client to the server to the DB and back again. They just go to the mock server, which is told to return a certain object/value for a given call, and to expect that certain calls will be made.

Chris Doggett
And why use Moq instead of writing a FakeServer class by hand?
Martinho Fernandes
We've already got the server written to a specific interface. Mocking basically fills in the stubs for the methods we don't use. If we only want to test one or two methods, we don't have to create an entire implementation of the interface.
Chris Doggett
+1  A: 

A mocking framework relieves you of the burden of creating unique mock objects, that return data specific to your test case. They also allow for the testing of whether or not certain methods or properties have been accessed or modified (by consumers of the mock object). This allows you to more easily test, and thus enforce, certain expected behavior.

Mock frameworks are just tools. Sure you could do it manually, but you may find yourself spending time maintaining classes that are just for testing.

A similar question is available here.

Ryan Emerle
A: 

You may want to read this Manual Mocking: Resisting the Invasion of Dots and Parentheses

Daniel Moura
A: 

Mocking allows you to control the input coming in to verify that the output occurs correctly. For instance, you can pass in data you know will error, and then test that your methods actually throw that error, and you can pass in valid data and test that the valid data does get returned from the object.

There are more frameworks than just Moq; there are RhinoMocks and TypeMock (my personal favorite though it does cost money unless it's an open source project) to name a few.

Brian