skip to Main Content

I am building an application using Laravel, a bug was found, so, I would to know where to put the hotfix test. I have this tests structure:

Tests -> Feature -> Http -> MyControllerTest.php, more files…
Tests -> Unit-> Models-> MyModelTest.php, more files…

I know that I have to write a test for that bug, but I don’t know where to put it, it is related to the authorization using tokens, some routes are not being protected, and it looks like I need to write a custom middleware I think, I am not sure yet, I was thinking to put it in a differente file in Feature/Http folder, something like HotFixTest.php?, or maybe in the same MyControllerTest.php file, I don’t know where? what can I do? thanks.

2

Answers


  1. If you are going to fix that given BUG simply implementing a middleware, you can "cover" that implementation by simply written a UnitTest.

    I have no context, but given a new file at /app/Http/Middleware/TokenGuardMiddleare you can create the test /test/unit/Http/Middleware/TokenGuardMiddleare

    Login or Signup to reply.
  2. What I do is, depending on what you have to do to fix the bug, I would:

    • Instead of having Tests -> Feature -> Http -> MyControllerTest.php it should be Tests -> Feature -> Htpp -> UserRegistration (let’s say that MyController allows a user to register).
      • This means you should write folder names for FEATURE tests like what they are, a feature, not a file, you don’t care about the implementation (and that is how you are writting HTTP tests)
    • If the fix requires you to fix the controller without creating new code outside it (no middlewares or anything similar), just create a new test inside the Test class
    • If the fix requires a new middleware or something outside the controller, but "interacts" with the controller, for example, a middleware, a form request, etc (they are not inside or in the controller, but complements it), then create a new test class related to that feature or write the test inside the existing class like in the previous point.
      • Why?: because you are testing a feature. If you have URL /user/123 and the bug is that it has no authntication, you have to create a new test in there, that will do a GET or whatever to /user/123 and you should expect a 401 or 403, but another normal test that if you do send the token, you get a 200 or whatever is successful for you. See that it is TIED to the feature. You have to do this for each route, do not unit test the middleware
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search