Question

I am trying to find out the best way to Assert, should I be creating an object with what i should return and check that it has equal to the expected result ?

Or should I be running a method against a mock to ensure that the method was actually called.

I have seen this done both ways, I wondered if anyone has any best practices for this.

Of course, it's quicker and easier to write a unit test to assert that a method was called on the mock but quicker and easier is not always the best way - although sometimes it can be.

What does everyone assert on, that a method has been called or assert the results that were returned ?

Of course its not best practice to do more than 1 assert in a unit test so maybe the answer is to actually assert the results and that the method was called ? So I would create 2 unit tests, 1 to check the results and 1 to check that the method was called.

But now thinking about this, maybe this is going too far, if I get a result that I suppose I can assume that my mock method was called.

Was it helpful?

Solution

In between testing that a method has been called and testing the value that it returns is another possibly more important test: that it was called with the correct parameters.

A very common case these days would a method you're writing that uses some HTTP library to retrieve data from a REST API. You don't want to actually make HTTP requests in your tests so you mock the HTTP client get() method. On the one hand your mock might just return some canned JSON response like (Using RSpec in Ruby as an example):

http_mock.stub(:get).and_return('{result: 5}')

You then test that you can properly parse this and return some correct value based on the response.

You could also test that the HTTP get() method is called, but it's more important to test that it's called with the correct parameters. In the case of an API, your method probably has to format a URL with query parameters and you need to test that it does that correctly. The assertion would look something like (again with RSpec):

http_mock.should_receive(:get).with('http:/example.com/some_endpoint?some_param=x')

This test is really a prerequisite to the previous test, and simply testing that get() was called wouldn't confirm much. For instance, it would tell you if you were incorrectly formatting the URL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top