简体   繁体   中英

Larabook tests error - cannot login user

I have gone through the Larabook series at Laracasts but I can't seem to find a solution to my problem. My code is working perfectly in the browser but my functional tests are failing because the user is not logged in. This is the SignUpTest and it passes until the last bit $I->assertTrue(Auth::check());

$I = new FunctionalTester($scenario);
$I->am('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:', 'john@example.com');
$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'
]);


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

The problem is that it does not log in the user so Auth::check() returns false. I followed Jeffery to the letter and I can't figure out why it is not working.

For the SignInCept, I have the same issue

$I = new FunctionalTester($scenario);
$I->am('a larabook member');
$I->wantTo('login to my larabook account');

$I->signIn();
$I->seeInCurrentUrl('/statuses');
$I->see('Post a status');
$I->assertTrue(Auth::check());

The signIn method

public function signIn()
    {
        $email = 'foo@example.com';
        $username = 'Foobar';
        $password = 'foo';

        $this->haveAnAccount(compact('email', 'username', 'password'));

        $I = $this->getModule('Laravel4');

        $I->amOnPage('/login');
        $I->fillField('email', $email);
        $I->fillField('password', $password);
        $I->click('Sign In');
    }

The tests fail at the assertTrue because again, it takes in the credentials, even goes to the status url but somehow the user is not logged in yet.

All my functional tests fail whenever I call the signIn method. Can anyone please tell me why it is not Logging in a user?

The issue was that by default in app\\config\\testing\\session.php the driver is set to array which means that no persistence of data occurs. So the solution is to change this to either database or cookie or any of the ones provided in the php_doc section. In my case I used cookie and all my tests passed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM