gpt4 book ai didi

php - 如何通过 Codeception 在 Laravel 5 中使用数据库事务?

转载 作者:行者123 更新时间:2023-11-29 03:28:07 25 4
gpt4 key购买 nike

我正在使用 Codeception 测试我的 Laravel5 API,我注意到 Codeception 附带的回滚功能有时会变得有点慢,因为我提供了巨大的 SQL 转储。一种解决方案是使用 SQLite 使其更快,但我希望坚持使用 MySQL。

我认为 Laravel 5 中的数据库事务功能可以帮助加快速度。但是我正在努力了解如何将它与代码一起使用,并且它也可以首先使用。

<?php 

$I = new ApiTester($scenario);
$I->wantTo('create a new comment');
$I->haveHttpHeader('Authorization', 'Bearer ' . file_get_contents('tests/api/token'));
$I->sendPost('comments', [
"document_id" => "2",
"comment_type_id" => "2",
"user_id" => "2",
"body" => "Testing"
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['status' => 'success']);
$I->seeInDatabase('comment', [
"document_id" => "2",
"comment_type_id" => "2",
"user_id" => "2",
"body" => "Testing"
]);

这是我写的一个codeception测试。现在我尝试添加:

\DB::beginTransaction(); // At the start
\DB::rollback(); // At the end

但是没有用。

我什至试图用这个来结束整个测试:

\DB::transaction(function () {
// Whole Test here
});

但即使这样也没有成功。运行测试后,我仍然在数据库中看到新评论。那么我做错了什么以及如何解决它?

最佳答案

您的tests/api.suite.yml 文件是什么样的?

它对我有用,我的看起来像这样:

class_name: ApiTester
modules:
enabled:
- Helper\Api
- Asserts
- Laravel5:
environment_file: .env
- REST:
depends: Laravel5

添加这个可能会有帮助 - 但我不需要

- Laravel5:
cleanup: true

关于php - 如何通过 Codeception 在 Laravel 5 中使用数据库事务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33688889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com