Search code examples
unit-testingmockingphpunitmockery

Mocking large return results


I am using mockery to mock the returned results of method calls to a database (so I am not actually hitting the database) during unit tests using phpunit.

One method in particular returns a massive array of results. My other tests which mock the return values return the actual expected result which often is a basic string.

For these very large results, should I return the large results in the mock (possibly including a text file with the result?) or should I be asserting them in a different way, perhaps only asserting the first element in the array or asserting the structure of the array?

What is the best practice for mocking the return of large data?


Solution

  • Unless you want to test the performance, and there is no expected difference in behaviour between 2, 10 or 100 results, there is no need to test against a large set.

    Generally, you want to identify where the behaviour differences lie, and test around them. So for a results set, that would usually mean testing with 0, 1 and 2 results, as anything bigger than that gets the same treatment anyway.

    And indeed, you want to validate the structure of the whole result set as well. This is a whole lot easier to do if you just test with one result at a time, which you should do, as the 'unit' in unit testing might suggest.