narwen's avatar

Unit Testing in Laravel 5

I am trying to test my code and I have followed the documentation and I am not getting it quite right.

I have created a UserTest.php file to check the all the process of creating the users and CRUD

    <?php

    class UserTest extends TestCase {
    public function testSomethingIsTrue()
     {
         $response = $this->action('POST', 'RoleController@create_permission');

     }

}

But when I run phpunit, it return 'OK' for everything. Is there any good tutorial available anywhere.?

0 likes
4 replies
olimorris's avatar

I wonder if PHPUnit is just running the ExampleTest.php contained within the core Laravel Repository. Where have you saved your code to?

Might be worth checking your phpunit.xml to check that it has the directory to where your tests sits, listed under <testsuite> tags.

Also, write a test that you know will definitely fail:

/** @test */
public function it_correctly_loads_the_homepage()
{
    $this->call('GET', '/thistestaintgonnaworkinamillionyears');
    $this->assertResponseOk();
}

to check that PHPUnit is loading your test files.

In terms of good tutorials...my man/lady you're in the right place. Jeffrey is a PHP testing guru and his book Laravel Testing Decoded is simply excellent along with all his testing videos:

  1. https://leanpub.com/laravel-testing-decoded
  2. https://laracasts.com/collections/testing-in-php
  3. https://laracasts.com/series/intuitive-integration-testing

I found the series which helped me the most in terms of understanding how to incorporate testing into my daily workflow was the Larabook series:

  1. https://laracasts.com/series/build-a-laravel-app-from-scratch

The one that's changed for me since Larabook is to use PHPUnit in place of Codeception and this largely due to Jeffrey's Laracasts Integrated package which you can learn more about in link 3 above.

narwen's avatar

@olimorris Yeah, I included the test that will definitely fail but it still is returning ok. I checked the tag and it is on proper directory? How can I fix it?

olimorris's avatar

@narwen Have you tried running the failing test and seeing that that fails as you expect? Perhaps first try pasting my code above into ExampleTest.php, verifying it fails and then putting it into your existing test to try and achieve the same result.

bingvanmoorsel's avatar

Shouldn't there be an assertion in your statement? like this it wont fail cause its not expecting anyting ?

Please or to participate in this conversation.