Search code examples
phpvalidationlaravelfunctional-testing

Laracasts\Validation\FormValidationException: Validation failed


In the 10th video of the Build Larabook from scratch Laracasts tutorial, the author runs a functional test that turns green:

enter image description here

where t is an alias for vendor/bin/codecept run functional in the Vagrant vm.

However, when I run the same test, it turns red:

vagrant@homestead:~/larabook$ vendor/bin/codecept run functional
Codeception PHP Testing Framework v2.0.7
Powered by PHPUnit 4.3.4 by Sebastian Bergmann.

Functional Tests (1) ----------------------------------------------------------------------------------------
Trying to sign up for a Larabook account (SignUpCept)                                                   Error
-------------------------------------------------------------------------------------------------------------


Time: 3.56 seconds, Memory: 18.75Mb

There was 1 error:

---------
1) Failed to sign up for a larabook account in SignUpCept (/home/vagrant/larabook/tests/functional/SignUpCept.php)
Couldn't click "Sign Up":
Laracasts\Validation\FormValidationException: Validation failed

Scenario Steps:
9. I click "Sign Up"
8. I fill field "Password Confirmation:","demo"
7. I fill field "Password:","demo"
6. I fill field "Email:","[email protected]"
5. I fill field "Username:","JohnDoe"
4. I see current url equals "/register"
3. I click "Sign Up!"


FAILURES!                          
Tests: 1, Assertions: 1, Errors: 1.

Here is the content of my SignUpCept.php file:

<?php

$I = new FunctionalTester($scenario);
$I->am('a guest');
$I->wantTo('sign up for a Larabook account');

$I->amOnPage('/');
$I->click('Sign Up!');
$I->seeCurrentUrlEquals('/register');

$I->fillField('Username:', "JohnDoe");
$I->fillField('Email:', "[email protected]");
$I->fillField('Password:', "demo");
$I->fillField('Password Confirmation:', "demo");
$I->click('Sign Up');

$I->seeCurrentUrlEquals('');
$I->see('Welcome to Larabook!');
$I->seeRecord('users', [
    'username' => 'JohnDoe',
    'email' => '[email protected]'
]);

$I->assertTrue(Auth::check());

I must be missing something, but I cannot figure out what.

Any idea?


Solution

  • The problem was coming from the fact that the test was using the same data (username, email and password) as a user that was already in the database.

    Since the validation checks the uniqueness of each new user, the test was failing.

    I just removed the existing user from the database and the test passed.