Testing is a very important part of software development, and it doesn’t just ends at you writing tests for your perfectly working code but writing tests before you write your perfectly working code i.e. follow TDD - Test Driven Development. And while you are doing all this you might have faced the problem where running your tests suite is taking a lot of time and hence increasing your development time. That’s one reason why you would opt for mocking in your tests.
When unit testing, many times we find that the unit being tested depends on other units for its functioning. These units can sometimes be difficult to work with because they might be dealing with a database, file IO or maybe an HTTP API. All of these can be slow at times, especially an HTTP API and increasing testing time as well as you might not want to hit an external HTTP API each time you run tests. Here comes in the use of test-doubles. In Ruby RSpec-mocks is one way to create these. Let’s say you are working on a twitter app that has a PostController -
1 2 3 4 5 6 7 8 9 10 | |
Here the unit being tested is “create” method and hence we do not care about the implementation of the dependencies like Post.
Now lets write the test for PostController.create() -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
In the above example, you can see we have tried to verify if the unit being tested makes calls that it is supposed to and returns the expected value. By setting the expectation on :save we ensure that if it is not called then the test will fail.
Here there is no need to worry about the user being queried for its details that are to be used in creation of a post, or how the post is actually going to save the details into the database and at the same time we are able to confirm that the functions required by the method create are being used by it.
RSpec-mocks provides a lot more similar functionalities that can be used to test more complex functions easily. For more details have a look at the documentation.
Mocking is a very opinionated topic just like testing itself. Some people believe that ideally all tests should be properly mocked while some believe otherwise. I have found mocking helpful because of the reasons I have mentioned above. The downside of mocking that I have felt is that sometimes you need to know some of the implementation details beforehand. I think there is no problem if you use mocking or not, the main thing to note is that you test.