meto1080's avatar

Laravel testing cannot recognise route when call the same test function at the second time

Hi Guys,

I am newbee of Laravel testing. I just got an route not define error when I call the same test function at the second time.

Story:

  1. I put the login test into the TestCase.php file for reusability in all test files.
  2. When it is called by the first test case, everything is fine. I.E. loginSuccess, loginFailed functions below.
  3. But when the second test case pickup the same testing above, it give me error that routes not defined.

More details, please refer to below code and error message in the end.

Will appreciate if someone can point out where I am doing wrong.

/**
     * Shared function for login fail test.
     */
    public function loginFail() {
        $this->visit(route('auth.getLogin'))
            ->see('Welcome to VMS')
            ->type('notexist@gmail.com', 'email')
            ->type('asdfasdfasdfa', 'password')
            ->press('Login')
            ->seePageIs(route('auth.getLogin'))
            ->see('These credentials do not match our records');
    }
    /**
     * Shared function for login fail test.
     */
    public function loginSuccess() {
        $this->visit(route('auth.getLogin'))
            ->see('Welcome to VMS')
            ->type(Config::get('constants.VISDOM_ADMIN_EMAIL'), 'email')
            ->type(Config::get('constants.VISDOM_STANDARD_PASSWORD'), 'password')
            ->press('Login')
            ->dontSee('These credentials do not match our records');
    }
/**
     * A basic functional test example.
     *
     * @return void
     */
    public function testAddUserInPage()
    {
        $this->loginSuccess();
        $this->visit(route('users.add'))
             ->type($this->faker->firstName, 'first_name')
             ->type($this->faker->lastName, 'last_name')
             ->type($this->faker->email, 'email')
             ->type(Config::get('constants.VISDOM_STANDARD_PASSWORD'), 'password')
             ->type(Config::get('constants.VISDOM_STANDARD_PASSWORD'), 'password_confirmation')
             ->type('<div>als,dfjaslkdf</div>', 'email_signature')
             ->press('Add User')
             ->see('User created successfully');
        $this->reset();
    }
There were 2 errors:

1) UserAuthBddTest::testUserAuthSuccess
InvalidArgumentException: Route [auth.getLogin] not defined.

/home/vagrant/Code/vms-v4/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:306
/home/vagrant/Code/vms-v4/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:614
/home/vagrant/Code/vms-v4/tests/TestCase.php:43
/home/vagrant/Code/vms-v4/tests/users/UserAuthBddTest.php:24

2) UserAuthBddTest::testUserAuthFail
InvalidArgumentException: Route [auth.getLogin] not defined.

/home/vagrant/Code/vms-v4/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:306
/home/vagrant/Code/vms-v4/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:614
/home/vagrant/Code/vms-v4/tests/TestCase.php:30
/home/vagrant/Code/vms-v4/tests/users/UserAuthBddTest.php:34

FAILURES!
Tests: 4, Assertions: 11, Errors: 2.

Above errors are all returned from the second test case.....

See my second test case, nothing funny really.

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

<?php
/**
 * @file
 *
 * BDD test for user authentication.
 */

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class UserAuthBddTest extends TestCase
{
    use DatabaseTransactions;

    public function setUp() {
        parent::setUp();
    }

    /**
     * Test login fail.
     *
     * @return void
     */
    public function testLoginFail()
    {
        $this->loginFail();
    }

    /**
     * Test login fail.
     *
     * @return void
     */
    public function testLoginSuccess()
    {
        $this->loginSuccess();
    }

}

0 likes
8 replies
ifpingram's avatar

@meto1080 hmm - can you properly format the question please, it's very difficult to read! Thanks!

meto1080's avatar

Hi, @ifpingram Thanks for your reply. Apologise for the poor format.

I am a bit struggling how to make the format nice as it is picked up automatically.

However, I put all description on the top and rewrite it. Hope it make sense.

ifpingram's avatar

@meto1080 it's missing the setUp call, which constructs the parent set up. Your second test class should read:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class UserAuthBddTest extends TestCase
{
    use DatabaseTransactions;

    protected $faker;

    public function setUp() {
    parent::setUp();
    }

    /**
     * Test auth success.
     *
     * @return void
     */
    public function testUserAuthSuccess()
    {
        $this->loginSuccess();
    }

    /**
     * Test auth fail.
     *
     * @return void
     */
    public function testUserAuthFail()
    {
        $this->loginFail();
    }

}

meto1080's avatar

@ifpingram Please see below.

<?php
/**
 * @file
 *
 * This file contain routes related to users or user authentications.
 */

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

// User login page.
Route::get('auth/login', array(
    'as' => 'auth.getLogin',
    'uses' => 'Auth\AuthController@getLogin',
));

// Process user login.
Route::post('auth/login', array(
    'as' => 'auth.postLogin',
    'uses' => 'Auth\AuthController@postLogin',
));

// User logout page url.
Route::get('auth/logout', array(
    'as' => 'auth.getLogout',
    'uses' => 'Auth\AuthController@getLogout',
));

// Route group for user related routes.
Route::group(['prefix' => 'users', 'middleware' => 'auth', 'as' => 'users.'], function ()
{
    Route::get('add', array(
        'as' => 'add',
        'uses' => 'UserController@add',
    ));

    Route::post('add', array(
        'as' => 'store',
        'uses' => 'UserController@store',
    ));
});

Please or to participate in this conversation.