简体   繁体   English

无法在Codeception接受_bootstrap.php中运行Auth :: loginUsingId(1)

[英]Can't run Auth::loginUsingId(1) in Codeception acceptance _bootstrap.php

I'm using Laravel and Codeception to do acceptance testing. 我正在使用Laravel和Codeception进行验收测试。 Most of my tests require users to be logged in, so I wanted to extract those behaviours to the _bootstrap.php file inside the acceptance folder. 我的大多数测试都要求用户登录,因此我想将这些行为提取到接受文件夹中的_bootstrap.php文件中。 I understand this is the practice for running setup prior to your tests, in Codeception. 我了解这是在Codeception中在测试之前运行安装程序的做法。

I can confirm that my users table exists (I'm viewing it using Navicat), and that a user with an id of 1 exists. 我可以确认我的用户表存在(我正在使用Navicat查看它),并且存在ID为1的用户。 Codeception, to the best of my knowledge, is properly configured to use this database. 据我所知,Codeception已正确配置为使用此数据库。 I also have included the Laravel4 and Db modules for the acceptance suite. 我还为接受套件包括了Laravel4和Db模块。 I receive the following error when I try to run my acceptance suite, or any individual acceptance Cept. 当我尝试运行我的接受套件或任何单个接受Cept时,我收到以下错误。

console : 控制台

[Illuminate\Database\QueryException]
SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "users" does not exist
LINE 1: select * from "users" where "id" = $1 limit 1
                      ^ (SQL: select * from "users" where "id" = 1 limit 1)

I get this error regardless of the contents of any of the acceptance Cepts. 无论任何接受节的内容如何,​​我都会收到此错误。 Even a simple $I->amOnPage('/') , or $I->comment('test') still results in the error. 即使是简单的$I->amOnPage('/')$I->comment('test') $I->amOnPage('/')导致错误。 I believe the error occurs as soon as it encounters the command in the _bootstrap.php file. 我相信该错误会在_bootstrap.php文件中遇到命令后立即发生。 Somehow it's able to access my database. 它以某种方式能够访问我的数据库。 I should note that I've also tried various combinations of enabling and disabling the populate and cleanup settings for the Laravel4 and Db modules, to no avail. 我应该注意,我还尝试了各种启用和禁用Laravel4和Db模块的填充和清除设置的组合,但都没有用。

Here is my acceptance/_bootstrap.php file: 这是我的accepting / _bootstrap.php文件:

<?php

Auth::loginUsingId(1);

codeception.yml: codeception.yml:

actor: Tester
paths:
    tests: tests
    log: tests/_output
    data: tests/_data
    helpers: tests/_support
settings:
    bootstrap: _bootstrap.php
    colors: true
    memory_limit: 1024M
modules:
    config:
        Db:
            dsn: 'pgsql:host=localhost;dbname=myapp'
            user: 'someuser'
            password: 'somepass'
            dump: tests/_data/dump.sql

acceptance.suite.yml: accept.suite.yml:

class_name: AcceptanceTester
modules:
    enabled:
        - AcceptanceHelper
        - Laravel4
        - PhpBrowser
        - Db
    config:
        PhpBrowser:
            url: 'http://localhost'
        Laravel4:
            cleanup: false
        Db:
            populate: true
            cleanup: false

Firstly, you should not use Laravel4 module for Codeception acceptance tests. 首先,您不应将Laravel4模块用于Codeception 验收测试。 It is only for functional and unit tests. 仅适用于功能测试和单元测试。 See this answer for more info on this issue. 有关此问题的更多信息,请参见此答案。

To answer your specific question about automatically logging in your user on your acceptance tests - this is how I do it: 要回答有关在验收测试中自动登录用户的特定问题-这是我的方法:

/tests/_support/AcceptanceHelper.php /tests/_support/AcceptanceHelper.php

<?php
namespace Codeception\Module;
use \AcceptanceTester;

class AcceptanceHelper extends \Codeception\Module
{
    // This is my default user I use for all my tests
    public function loginUser(AcceptanceTester $i)
    {
        $this->login($i, 'user@test.com', 'tester');
    }  

    // And this function lets me login a specific user if I need someone else
    public function login(AcceptanceTester $i, $email, $password)
    {
        $i->amOnPage('/login');
        $i->fillField('email', $email);
        $i->fillField('password', $password);
        $i->click('submit');
        $i->seeCurrentUrlEquals('/dashboard');
    }

    public function logoutUser(AcceptanceTester $i)
    {
        $i->amOnPage('/logout');
        $i->seeCurrentUrlEquals('/login');
        $i->see('You have been logged out');
    }
}

/tests/acceptance/ExampleCest.php /tests/acceptance/ExampleCest.php

<?php
use \AcceptanceTester;

class ExampleCest
{
    public function _before(AcceptanceTester $i)
    {
        $i->loginUser($i);
    }


    public function tryToViewDashboard(AcceptanceTester $i)
    {   
        // User is logged in here 
        $i->amOnPage('/dashboard');
    }

    public function tryOtherExample(AcceptanceTester $i)
    {   
        // User is logged in here 
        $i->amOnPage('/dashboard');
    }

    public function tryMoreInfo(AcceptanceTester $i)
    {   
        // User is logged in here 
        $i->amOnPage('/dashboard');
    }
}

Note: I use a smallcase $i instead of $I - I find it easier to type - so you might need to change all the $i to $I in your code 注意:我使用小写的$i而不是$I我发现键入更容易-因此您可能需要在代码中将所有$i更改为$I

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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