Building a Blog in Laravel – Part2 – Configure the Tests

Oct 19,2017 at 01:00 pm By Ben Hutton

Next is to configure the tests.  This article assumes you have a working Laravel setup, if not please go to my previous article. As per TDD you write your tests first.  Laravel makes this really easy, once you know how.  All you have to do is run the following command to setup a test class.

# php artisan make:test RouteTest

This will create the file RouteTest.php under tests/Feature.  Within this you can create the routing and authentication tests as follows:


namespace Tests\Unit;

use Tests\TestCase;
use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class RouteTest extends TestCase
    use DatabaseMigrations;

    public function testRoot()
        $response = $this->get('/');

    public function testHomeShowsLogin()
        $response = $this->get('/home');

    public function testHomeLogsIn()
        $password = '1qazwsx'; // Our password

        $user = factory(User::class)->create([
            'name' => 'fred',
            'email' => '',
            'password' => bcrypt($password) // Save the encrypted

            ->withSession(['foo' => 'bar'])
            ->assertSee('You are logged in!');



Unit Tests

This took some time to get working.  It appears that in Laravel 5.5 the method of testing authentication has changed from previous versions.  While going through a few online forums the only tests I could find were functional in nature.  These of course require the web server being active.  This is not ideal when developing an application.  The functional tests should only be used at the end of a successful TDD flow.  Basically do you TDD with our unit tests in isolation, and only when you have your unit tests working successfully, and only then run your functional tests.

Functional Tests

Laravel 5.5 uses Dusk for functional tests.  This has selenium/webdriver built in which is quite nice.  Now to get this working you will need to run the following commands:

# php artisan migrate
# php artisan make:auth
# php artisan dusk:install
# php artisan dusk:make LoginTest

Now before you can run the functional tests you will need the web server running.  You can do this with the following command:

php artisan serve

Laravel development server started: <>

[Wed Oct 18 08:37:54 2017] [200]: /css/app.css

[Wed Oct 18 08:37:54 2017] [200]: /js/app.js

[Wed Oct 18 08:37:56 2017] [200]: /js/app.js

[Wed Oct 18 08:37:56 2017] [200]: /css/app.css

Now take a note of what address it is listening on.  In my case which is the default it is listening on “”.  This is where I ran into a bit of a gotcha.  By default your environment setting doesn’t specify the port (in this case 8000).  If not set it will use port 80 since this is the standard.  However the error you get is not very intuitive.  When running the tests you will get something like the following if you cannot reach the web server:

no such element: Unable to locate element: {"method":"css selector","selector":"body textarea[name='email']"}

If this is the case make your the APP_URL setting in .env is point to your correct URL.  In my case I had to change it from:




If everything goes well you should get something like the following when you run your unit tests.

For Unit Tests run:

# phpunit
PHPUnit 6.4.2 by Sebastian Bergmann and contributors.

.....                                                               5 / 5 (100%)

Time: 820 ms, Memory: 24.00MB

OK (5 tests, 9 assertions)

For Functional Tests run:

# php artisan dusk

PHPUnit 6.4.3 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 3.88 seconds, Memory: 18.00MB

OK (2 tests, 4 assertions)

Leave a comment

Login to Comment

Copyright © 2017 | Ben Hutton