我使用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:
提供没有附加信息的答案将会非常棘手。你有任何PHP错误?你试过用'--debug'(类似于'codecept run --debug')来运行代码解析吗? –
是的,我用--debug运行,我将用输出更新描述。不,我没有得到任何错误,PHPUnit正在退出,没有任何错误消息,这是什么使这很难调试。 –
也许你可以尝试创建一个PHPUnit测试并尝试获取更多信息。另外,尝试添加'error_reporting(E_ALL); ini_set('display_errors','1');'在脚本的顶部。 –