我正在使用Codeception和fixtures来测试我的API,但似乎固定装置仅适用于第一次测试。这是我的测试:
class ActionCest extends BaseTestCase
{
public function _fixtures()
{
return [
'profiles' => [
'class' => UserFixture::className(),
// fixture data located in tests/_data/user.php
'dataFile' => codecept_data_dir() . 'user.php'
],
'actions' => [
'class' => ActionFixture::className(),
'dataFile' => codecept_data_dir() . 'action.php'
],
];
}
public function createAction(ApiTester $I)
{
$user = $I->grabFixture('profiles', 'user1');
$I->wantTo('Add action');
$I->haveHttpHeader('Authorization', 'Bearer '. $user['auth_key']);
$payload = [
'action_id' => 123,
'saved' => true,
'viewed' => false,
'complete' => false,
];
$I->sendPOST('/action/save', $payload);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
$I->seeResponseContainsJson($this->buildResponsePayload($payload));
}
public function getAction(ApiTester $I)
{
$user = $I->grabFixture('profiles', 'user1');
//$action = $I->grabFixture('actions', 'action1');
$I->wantTo('Retrieve action');
$I->haveHttpHeader('Authorization', 'Bearer '. $user['auth_key']);
$I->sendPOST('/action/get-by-id/1');//'. $action['action_id']);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
$I->seeResponseContainsJson($this->buildResponsePayload($payload));
}
}
在上面的示例中,第一个测试将通过OK。但是,由于用户未经过身份验证,第二次测试将失败。我假设在第一次测试后用户已从数据库中删除。
我如何克服这个?
答案 0 :(得分:0)
答案是用以下方法替换_fixtures()
方法:
public function _before(ApiTester $I)
{
$I->haveFixtures([
'users' => [
'class' => UserFixture::className(),
'dataFile' => codecept_data_dir() . 'user.php',
],
'actions' => [
'class' => UserActionFixture::className(),
'dataFile' => codecept_data_dir() . 'action.php',
],
]);
}