夹具不适用于后续测试
Fixtures not available for subsequent tests
我正在使用 Codeception 和固定装置来测试我的 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。但是,由于用户未通过身份验证,第二次测试将失败。我假设用户在第一次测试后已从数据库中删除。
我该如何克服这个问题?
答案是用下面的方法替换_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',
],
]);
}
我正在使用 Codeception 和固定装置来测试我的 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。但是,由于用户未通过身份验证,第二次测试将失败。我假设用户在第一次测试后已从数据库中删除。
我该如何克服这个问题?
答案是用下面的方法替换_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',
],
]);
}