Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Purpose of Mocking

What is the purpose of mocking?

I have been following some ASP.NET MVC tutorials that use NUnit for testing and Moq for mocking. I am a little unclear about the mocking part of it though.

like image 866
Ronnie Overby Avatar asked Jun 16 '09 15:06

Ronnie Overby


People also ask

Is mocking a good thing?

Mocking is a very common testing mechanism, and it is a bad idea. This post details why you should not use mocking, and why and how you should write integration tests instead. TL;DR: Mocking provides false confidence by hiding real failures.

What does mocking a class allow you to do?

Mocking means creating a fake version of an external or internal service that can stand in for the real one, helping your tests run more quickly and more reliably. When your implementation interacts with an object's properties, rather than its function or behavior, a mock can be used.

What happens when we mock an object?

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.

What are two reasons to use mock objects in unit tests?

Typically you write a mock object if: The real object is too complex to incorporate it in a unit testing (For example a networking communication, you can have a mock object that simulate been the other peer) The real object is not yet available.


6 Answers

The purpose of mocking is to isolate the class being tested from other classes.

This is helpful when a class :

  • connects to an external resource (FileSystem, DB, network ... )
  • is expensive to setup, or not yet available (hardware being developed)
  • slows down the execution of the unit tests
  • has a non-deterministic behavior
  • has (or is) a user interface

It also makes it easier to test for error conditions, as your build your mock object so that it returns and error, throw an exception ...

The mock can record how it was invoked (function calls order, parameters) and this can be verified by the test. EDIT: For instance: The method you're testing sends a message, such as an IPC. The method of the mock object can record how many times it was invoked, the parameter he received (i.e. the message to be sent). Then the test can interrogate the mock object and assert on the number of messages sent, the content of the message ... Likewise, the mock object can record the methods that are called in a log string and the test can retrieve that string and assert on it.

Do not abuse of mock objects: test the behaviour rather than the implementation, or the unit tests will be too tightly coupled to the code, and brittle (break at refactoring).

Mock can be coded manually, or generated by a mocking framework.

like image 130
philant Avatar answered Oct 10 '22 01:10

philant


Mocking allows you to isolate your class under test from its dependencies. Generally, you create a mock for each dependency for the class under test and set up the mock to return expected values. You then provide the mock to your class under test instead of a real copy of the class that your class under test is dependent on. You can then use the mocking framework to check that the expected calls were made to the mock object(s) to ensure that your class under test is operating correctly.

like image 25
tvanfosson Avatar answered Oct 10 '22 01:10

tvanfosson


Its designed to poke fun at an individual instance from a group collection. Used a lot at irregular object gatherings.

like image 22
kingchris Avatar answered Oct 10 '22 02:10

kingchris


While mocking is usually understood as allowing for isolation of a class under test this isn't the main point of a mock (stubs are better for this). Instead we need to look at what happens when an object is told to do something which is one of 3 things..

  1. Direct Output - result of a method call
  2. Internal Changes - changes to the class during a method call
  3. Indirect Output - code under test calls a different class

State based testing is all about #1 and #2. #1 via looking at the result that the method gives you. #2 by accessing the objects internal state.

This leaves us with #3 for which there are two avenues we can take. The first is a by using a Mock and the second by using a Test Spy. The main difference is that on a Mock you create the expectations before executing the code under test and then have the mock verify them after, whereas with a Test Spy you execute the code under test and then ask the Test Spy if certain actions occurred.

So to sum all that up.. when you think about testing what a class does if you need to test indirect output (aka a call to another class) that is where Mocking comes into play.

like image 40
Shane Courtrille Avatar answered Oct 10 '22 00:10

Shane Courtrille


I'm new to mocking as well but I'll take a stab at this. In my experience mocking has two main benefits:

  • You can start working with objects before actually writing the implementation. You can define an interface and use mocking to work with the interface in unit tests or even code.
  • Mocking allows you to isolate the object under test in unit tests. With mock objects, you can completely control any objects that the object under test interacts with, therefore removing external dependencies from the test.
like image 23
Jamie Ide Avatar answered Oct 10 '22 00:10

Jamie Ide


"Mock" is a heavily overloaded term in testing & TDD circles. See Martin Fowler's article Mocks Aren't Stubs. A "proper" mock knows what values it's supposed to receive and lets you know when it doesn't get what was intended; this allows you to do interaction testing instead of state testing - you verify that the class under test is passing the correct messages to its collaborators, in the correct sequence. Interaction testing is quite different from conventional state testing and can be hard to get your head around. Keeping in mind that interaction testing is the point of mocks may make them easier to understand.

like image 28
Carl Manaster Avatar answered Oct 10 '22 01:10

Carl Manaster