2015-05-11 45 views
0

我使用Yii2作为我的应用程序框架,我从高级模板开始。我有我的应用程序的前端,后端和api入口点。我已经将前端,后端和api移出根目录并进入/ modules以保持混乱。Yii2和Codeception API测试_before

我正在尝试为我的API注册端点创建一个注册测试。在我运行测试之前,我需要确保我正在测试注册的电子邮件地址尚未在测试数据库中注册。

我正在使用Cest _before方法来查看是否可以找到具有测试电子邮件的成员模型,如果我找到一个然后将其删除。

问题是这导致测试在没有任何调试信息的情况下退出。

这是我应该如何做我的测试设置?我应该做些别的事情来清除任何旧的测试用户。

如果删除_before()设置功能,该测试将在我第一次运行时运行。

AuthenticationCest.php

namespace tests\codeception\api\api; 

use common\models\Member; 
use tests\codeception\api\ApiTester; 

class AuthenticationCest { 

    public $email = "[email protected]"; 

    //Check to see id the email is already used and delete it 
    public function _before(ApiTester $I) { 
     $member = Member::findOne([ 
      "email" => $this->email 
     ]); 

     if ($member) { 
      $member->delete(); 
     } 
    } 

    //Test that a member can register 
    public function registerTest(ApiTester $I) { 
     $I->wantTo('Register a new member via the API'); 

     $I->haveHttpHeader('Content-Type', 'application/json'); 

     $I->sendPOST('token', [ 
      'grant_type' => 'registration', 
      'client_id'  => 'testclient', 
      'client_secret' => 'testpass', 
      'email'   => $this->email, 
      'password'  => 'Test1234', 
      'given_name' => 'Test', 
      'family_name' => 'Member' 
     ]); 

     $I->seeResponseCodeIs(200); 
     $I->seeResponseIsJson(); 
    } 
} 

_bootstrap.php

defined('YII_DEBUG') or define('YII_DEBUG', true); 
defined('YII_ENV') or define('YII_ENV', 'test'); 

defined('YII_APP_BASE_PATH') or define('YII_APP_BASE_PATH', dirname(dirname(dirname(__DIR__)))); 

require_once(YII_APP_BASE_PATH . '/vendor/autoload.php'); 
require_once(YII_APP_BASE_PATH . '/vendor/yiisoft/yii2/Yii.php'); 
require_once(YII_APP_BASE_PATH . '/common/config/bootstrap.php'); 
require_once(YII_APP_BASE_PATH . '/modules/api/config/bootstrap.php'); 

Yii::setAlias('@tests', dirname(dirname(__DIR__))); 

这似乎是足以让我创建和删除单元测试的模型,而不是功能的API测试

api.suite.yml

class_name: ApiTester 
modules: 
enabled: [PhpBrowser, REST] 
config: 
    PhpBrowser: 
     url: http://api.yii.lan/ 
    REST: 
     url: http://api.yii.lan/v1/ 

编辑输出

$ ../vendor/bin/codecept run --steps --debug -vvv

Codeception PHP Testing Framework v2.0.13 
Powered by PHPUnit 4.6.6 by Sebastian Bergmann and contributors. 

[tests\codeception\common]: tests from /workspace/yii2/tests//codeception/common 


Tests\codeception\api.api Tests (3) ------------------------------------------------------------------------ 
Modules: PhpBrowser, REST, Db 
------------------------------------------------------------------------------------------------------------------------------------ 
Register via the API (tests\codeception\api\api\oauth\RegistrationCest::registerTest) 
Scenario: 
+0

提供没有附加信息的答案将会非常棘手。你有任何PHP错误?你试过用'--debug'(类似于'codecept run --debug')来运行代码解析吗? –

+0

是的,我用--debug运行,我将用输出更新描述。不,我没有得到任何错误,PHPUnit正在退出,没有任何错误消息,这是什么使这很难调试。 –

+0

也许你可以尝试创建一个PHPUnit测试并尝试获取更多信息。另外,尝试添加'error_reporting(E_ALL); ini_set('display_errors','1');'在脚本的顶部。 –

回答

0

如果您需要每次测试后明确的数据库,那么你需要使用的灯具。 夹具是测试的重要组成部分。他们的主要目的是在固定/已知状态下设置环境,以便您的测试可重复并按预期方式运行。 http://www.yiiframework.com/doc-2.0/guide-test-fixtures.html

阅读完文档后,您发现您已经拥有FixtureHelper,在高级模板中它应该位于/ tests/codeception/common/support/FixtureHelper中。

您应该在tests /.../ common/fixtures文件夹中生成名为MemberFixture.php的文件。之后,你应该在数据库中生成数据(几个成员将进行标准测试)。

在最后部分,您应该将FixtureHelper添加到youre .yml文件(functional.yml)中。比如我acceptance.yml看起来像

class_name: AcceptanceTester 
modules: 
enabled: 
    - PhpBrowser 
    - REST 
    - ApiHelper 
    - tests\codeception\common\_support\FixtureHelper 
    - Db 
config: 
    PhpBrowser: 
     url: http://localhost:8080 
    REST: 
     url: http://localhost:8080/frontend/web/index-test.php 
    Db: 
    dsn: 'mysql:host=localhost;dbname=y1db_test' 
    user: 'newproject' 
    password: 'newproject' 
    populate: false, 
    cleanup: false 

这就是所有,现在每次测试后,你就是成员表将被清除,并与灯具标准具数据填充。并且测试将是绿色的。

+0

如果您未在测试中创建用户,您将如何创建注册测试?在这一刻赛程无法帮助你。 –

+0

这个问题与五月现在的问题不同。所以我的回答和你的评论已经没有意义了。 –