CRUD Feature Testing in Laravel 5

Jeff Simons Decena
3 min readMar 7, 2018

After the articles on unit testing and negative unit testing, let us take a look into Feature testing. These tests are hitting your controllers and asserting if the responses are correct.

I would still use my carousel example from the previous article.

Create carousel feature test

In testing the feature of your application, make sure you segregate the admin UI from the front office UI testing. For my example, I namespace it with Admin and Front

Right now, our focus is the admin panel carousel CRUD. Let’s write the test now.

Let’s break this down a bit.

  • We need the ->actingAs() method to bypass the authentication middleware and mock the user as admin (if you don’t have Authorisation guard installed, you do not need this)
  • Then we fetch the route() for the create carousel page
  • Then we assert status as 200 if the page is existing
  • And lastly, the texts we would like to see on that page

Run your phpunit and see what happens.

PHPUnit 6.5.7 by Sebastian Bergmann and contributors.E                                                                   1 / 1 (100%)Time: 920 ms, Memory: 26.00MBThere was 1 error:1) Tests\Feature\Admin\Carousels\CarouselFeatureTest::it_can_show_the_create_carousel_page
InvalidArgumentException: Route [admin.carousel.create] not defined.

Correct. We have not defined the route in the web.php route file so it will show the error. Let’s define it in.

Break it down:

  • In my app/Http/Controllers I have other folders as well to structure my files / folders. I have Admin, Front and Auth
  • In my Admin namespace, I have also Carousels folder and inside it, is the CarouselController.php

Obviously, we need the actual controller, so to create it, just run in your terminal:

php artisan make:controller --resource Admin/Carousels/CarouselController

Once we have defined it and created the controller, run again the phpunit

PHPUnit 6.5.7 by Sebastian Bergmann and contributors.F                                                                   1 / 1 (100%)Time: 987 ms, Memory: 28.00MBThere was 1 failure:1) Tests\Feature\Admin\Carousels\CarouselFeatureTest::it_can_show_the_create_carousel_page
Failed asserting that '' contains "Title".

It works! The error for the routing is gone and we have a new error which leads us to think that the test is not able to see the word “Title” in the UI. Hmm Well, that is because we have not returned a view yet. The create() method is still empty and we need to fill it up.

then our view file in resources/views/admin/carousels/create.blade.php

The admin and carousels folders are not existing in the views folder so you need to create it.

Once you have created the blade file, run the phpunit again.

➜  git: phpunit --filter=CarouselFeatureTest::it_can_show_the_create_carousel_page
PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)Time: 810 ms, Memory: 28.00MBOK (1 test, 6 assertions)

Nice. Looks neat.

Now, if someone messes up your blade template, you would immediately know because this particular test will fail. Go look into your Github and check the blame and see who screws you up haha

Creating the actual carousel with post data

Now, let us test if the form in the blade template can actually create the carousel.

By doing that, write your test first! Don’t forget. No shortcuts.

Break it down

  • We assert that after the creation of the carousel, it should redirect it to the index page (list of carousel)
  • We assert also that we set a flash message Create carousel successful! message

This would error since our store() method is empty. Let us fill it in with:

Then run phpunit

➜  git: phpunit --filter=CarouselFeatureTest::it_can_create_the_carousel          
PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)Time: 993 ms, Memory: 28.00MBOK (1 test, 5 assertions)

Voila!

Just repeat the test in the other methods and you are good to go!

###

/jsd

--

--

Jeff Simons Decena
Jeff Simons Decena

Written by Jeff Simons Decena

Senior Fullstack Engineer, React Native developer and HL7 Health Informatics professional