【发布时间】:2018-07-22 22:47:24
【问题描述】:
我的 Laravel 应用程序中有以下测试文件:
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ApiAuthControllerTest extends TestCase{
use DatabaseTransactions;
public function testLogin(){
// Test login success
$response = $this->json('POST', '/login', array(
'email' => 'hello@yahoo.com',
'password' => 'sometext'
))->decodeResponseJson();
return $response['token'];
}
/**
* @depends testLogin
*/
public function testLogout($token){
// Test logout success
$this->json('DELETE', '/logout', array(
'token' => $token
))->assertReponseStatus(200);
}
}
我正在使用DatabaseTransactions 类将我的测试包装为事务,这样它们就不会写入我的数据库。我注意到使用这个类会将我的类中的每个单独的测试包装为一个事务。
我想将整个类包装为一个事务。在上面的示例中,我需要在测试注销请求时将登录请求生成的令牌持久保存在数据库中。
我将如何使用 Laravel 做到这一点?
【问题讨论】:
标签: laravel laravel-5 phpunit laravel-5.2 laravel-testing