dev3cplusnow's avatar

should I test my repositories in a unit manner?

Hi guys, my scenario is the following:

I'm developing an Restfull API, so I don't have any views. I'm working with the repositories pattern and trying to make TDD on this project. For my controllers I'm making functional tests (to verify if a user is inserted in the database for example), and acceptance tests (to verify if the response has the correct fields). My question is: in this two kinds of tests my repositories are touched and tested (I'm generating coverage reports with PHPUnit), so I have to do unit tests on the repositories or the validators in this case?

0 likes
2 replies
thepsion5's avatar

I usually use integration tests for repositories, just because the query builder and eloquent classes are really annoying to mock due to the fluent interfaces. I then mock the repositories themselves when unit-testing classes that depend on them.

Personally, I'd only use acceptance tests for controllers. Instead of touching the database directly, you should issue another API request and see if the change you made is properly reflected. For example:

  1. DELETE /api/resource/1

  2. Verify response HTTP code is 200

  3. GET /api/resource/1

  4. Verify response HTTP code is 404 (or however your API does it)

1 like
dev3cplusnow's avatar

It's a good practice. I will leave the repositories only with the integration tests and the unit tests for the custom components of the project. :-)

Please or to participate in this conversation.