Laravel 中 phpunit 对 JSON API 的测试问题,返回 Invalid JSON was returned from the route,正常用例应该怎么写?
- 在控制器中进行json的返回
return response()->json([ 'code' => $code, 'msg' => $msg, 'data' => $data ]);
-
使用文档中的示例进行phpunit测试
$response = $this->json('POST', '/index/index', ['name' => 'test']); $response ->assertStatus(200) ->assertJson([ 'code' => 0, ]);
- 因为在逻辑中所有的json接口code为0表示成功,但是这样的用例并不能通过phpunit的测试,返回信息为
Invalid JSON was returned from the route.
- 各种google之后的答案有说是因为csrf的问题,加了
use WithoutMiddleware;
也没用,不知道对于json的接口需要怎么样来进行phpunit测试?
推荐文章: