Testing Controllers, FormRequests, Policies, etc

Hi everybody, I just started studyng the fabulous world of automated testing. Main concepts seems pretty clear to me, but I still have some doubts on how to apply them to a Laravel-based application (I'm currently using Laravel 5.4), because I read opposite opinions and suggestions about it in the last week.

I have a new service which will be launching in a few months and my current task is to write tests to make it stable, fixing possible bugs on-the-fly. My first idea was to test controllers in every possible situation of input data in order to make it stable even if underlying implementation changes, but I'm pretty sure those are more feature tests than unit tests and that's actually too much work to be the right idea. Many people online suggest to avoid testing controllers at all because they shall hold very little logic, which is true but that "little logic" must tested anyway, in my opinion.

Insert now FormRequests and we keep adding up problems: many say they must not be tested (like controllers) because the rules are already tested by their own. I understand that point of view but the composition of the rules is, in fact, part of the application logic, so again it looks to me that they must be tested to avoid developers to alter validation behaviour introducing possible bugs.

Maybe it's all about the fact that they are tightly bounded with controllers methods and are used only one time in all the codebase without actually changing any data, but even knowing this I'm not really sure about how to behave: shall I test only the "best case" (all rules pass, all alterations took place) and "worst case" (all rules break, no alteration took place, all expected errors are returned)? Or shall I test only the "not trivial" scenarios?

On top of this, Authorization Policies jumps in. Unlike FormRequests, they can be "reused" (more that one controller method can be bound to a single policy rule).

After doing some tests, my idea on how to proceed is this:

  • ignore FormRequests testing
  • inspect every controller method of my API
    • for every call to a not laravel/PHP-native function check that method and add unit tests for it, repeat recursively if needed
    • write a feature test where the method is called and all validation constraint are ok (the best case, checking if updates actually took place)
    • write a feature test for every particular byproduct of the controller (eg. if it must send a notification, test that notification has been sent; if there is a secondary execution path, test the conditions and results of that path, etc.)
    • write one or more feature tests to enforce policies rules

Is this a good workflow? Is there a de-facto standard out there that I missed or is testing environment in laravel really this caotic? Of course from now on I'll follow TDD, my problem is just to catch up with the current codebase.

Yanga replied 9 months ago

USER Testing....

Paolo Caleffi replied 9 months ago

I'm not really sure about what you mean with this comment, can you explain?

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us