Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use a mocking framework instead of hand-rolling our mocks?

Tags:

mocking

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 ?

like image 397
Melursus Avatar asked Dec 18 '09 18:12

Melursus


People also ask

Why do we need mocking framework?

A complete mocking framework like JustMock enables developers to focus solely on testing the system under test and forget about the distracting mocking details. Mock objects are created automatically in memory when the tests are run based on the simple configuration in the unit test.

Why do we need mocking framework C#?

It is used to isolate each dependency and help developers in performing unit testing in a concise, quick, and reliable way. Creating mock objects manually is very difficult and time-consuming. So, to increase your productivity, you can go for the automatic generation of mock objects by using a Mocking Framework.

How is a mocking framework used for verification?

Verify methods were called Mocking frameworks add another way to test - checking whether methods were called, in which order, how many times and with which arguments. For example, let's say that a new test is required for adding a new user: if that user does not exist than call IDataAccess. AddNewUser method.

How does a mock work?

To isolate the behavior of the object you want to test you replace the other objects by mocks that simulate the behavior of the real objects. So in simple words, mocking is creating objects that simulate the behavior of real objects. In unit testing we want to test methods of one class in isolation.


1 Answers

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.)

like image 182
2 revs Avatar answered Sep 27 '22 18:09

2 revs