Ishatanjeeb's avatar

Showing error when test phpunit

Welcome to Git (version 1.9.5-preview20150319)

Run 'git help git' to display the help index. Run 'git help ' to display help for specific commands. sh: locale: command not found

Isha@TANJEEB /E/All Works/Laravel/Works/email $ phpunit PHPUnit 4.7.6 by Sebastian Bergmann and contributors.

F

Time: 4.37 seconds, Memory: 11.75Mb

There was 1 failure:

  1. AuthTest::a_user_may_register_for_an_account_but_must_confirm_their_email_address A request to [http://localhost/register/confirm] failed. Received status code [404].

E:\All Works\Laravel\Works\email\vendor\laravel\framework\src\Illuminate\Foundation\Testing\CrawlerTrait.php:230 E:\All Works\Laravel\Works\email\vendor\laravel\framework\src\Illuminate\Foundation\Testing\CrawlerTrait.php:153 E:\All Works\Laravel\Works\email\vendor\laravel\framework\src\Illuminate\Foundation\Testing\CrawlerTrait.php:50 E:\All Works\Laravel\Works\email\tests\AuthTest.php:27 C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176 C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129

Caused by exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in E:\All Works\Laravel\Works\email\vendor\lara vel\framework\src\Illuminate\Routing\RouteCollection.php:143

<?php

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

class AuthTest extends TestCase
{
    use DatabaseTransactions;

    /** @test */
    function a_user_may_register_for_an_account_but_must_confirm_their_email_address()
    {
        $this->visit('register')
            ->type('Tanjeeb', 'name')
            ->type('Tanjeeb@example.com', 'email')
            ->type('password', 'password')
            ->press('Register');

        $this->see('Please confirm your email address')
            ->seeInDatabase('users', ['name' => 'Tanjeeb', 'verified' => 0]);

        $user = User::whereName('Tanjeeb')->first();
        //$this->login($user)->see('Could not sing you in.');

        $this->visit("register/confirm/{$user->token}")
            ->see('You are now confirmed. Please login. ')
            ->seeInDatabase('users', ['name'=>'Tanjeeb', 'verified'=>1]);

    }
    protected function login($user = null)
    {
        $user = $user ?: $this->factory->create('App\User', ['password' => 'password']);
    }

}

0 likes
4 replies
captbrogers's avatar

Looking at the error, it seems that there isn't a valid route being used, either in your routes file or it could be on how things are configured with PHPUnit when you are trying to use $this->visit('register').

Can you verify that your routes file has that route, and that it is pointing to a valid controller where you get a response that isn't an error?

Ishatanjeeb's avatar
<?php

get('register', 'RegistrationController@register');
post('register', 'RegistrationController@postRegister');
//register/confirm/{$user->token}
get('register/confirm/{token}', 'RegistrationController@confirmEmail');

is that correct?

bobbybouwmann's avatar

Here is your mistake:

AuthTest::a_user_may_register_for_an_account_but_must_confirm_their_email_address A request to [http://localhost/register/confirm] failed. As you can see it's trying to access the register/confirm url, while you specified it should have a token as well! So you url must look like this

// Correct
http://localhost/register/confirm/2u3br23hrb23k

// Wrong
http://localhost/register/confirm

You can simply fix this by making the token optional!

// Note the question mark
get('register/confirm/{token?}', 'RegistrationController@confirmEmail');

Now in your controller you can do something like this

public function confirmEmail($token = null)
{
    if ($token = null) {
        throw new NotFoundHttpException;
    }   

    // Do what ever you want here!
}

Please or to participate in this conversation.