我在Laravel应用程序中具有以下测试文件:
use IlluminateFoundationTestingWithoutMiddleware;
use IlluminateFoundationTestingDatabaseMigrations;
use IlluminateFoundationTestingDatabaseTransactions;
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在setUp
/tearDown
上刷新APP实例。在phpunit中,这些功能是每个测试方法运行的。因此,使用交易意味着测试方法之间将没有持久性。
但是,您可以在testLogout
测试中再次生成令牌。由于您的注销测试依赖于存在的令牌,因此该方法本质上没有任何错误。