对于laravel API的自动测试我使用github actions上的'laravel'操作,由github actions制作的操作。测试一直失败告诉我从路由返回的无效JSON,预期响应代码200但得到500,无法读取null和无法在JSON
中找到属性状态我用laravel sanctum。这是csrf令牌问题吗?
我的动作yml: https://gist.github.com/I2EJeffrey/77df8faac1b0f86623e2e4449f98d858
我的响应函数:
* success response method.
*
* @return IlluminateHttpResponse
*/
public function sendResponse($result, $message, $code = 200)
{
$response = [
'success' => true,
'data' => $result, // result is most often one or 2 arrays
'message' => $message,
];
return response()->json($response, 200);
}
示例测试:
public function testSuccessfullyCreateAccommodationType()
{
$this->login(); // Login function that lots of tests need.
$response = $this->postJson('/api/v5/accommodations/1/types', ['accommodation_name'=>$this->createName()]);
$response
->assertJsonFragment(['success' => true])
->assertJsonStructure(['success', 'data' =>
[], 'message']); // The array is filled with keys
}
编辑:2个错误,我得到了使用withoutExceptionHandling
: https://gist.github.com/I2EJeffrey/da23bfbdf5fba155456bd799a34f6276
TTY mode requires /dev/tty to be read/writable.
编辑3:客户端模型和客户端种子。每当我运行测试时,一个mysql docker容器启动,得到一个db迁移并播种到它:https://gist.github.com/I2EJeffrey/39c779df217c9a75a7569f6fa3957d77https://gist.github.com/I2EJeffrey/41cbbdba8025d38547059d3a6f4d4392我的问题是,数据库没有得到种子由于没有添加$this->call(ClientSeeder::class);
到databaseseder。这会导致路由返回null,从而导致错误的json。