我是Laravel的新手,也是一般的单元测试。
我使用Plot so far
编写了一个简单的测试public function testGet(){
$this->get('/api/accounts/1')->assertStatus(200)->assertJsonStructure([
'status','data'
]);
}
运行测试时,它会生成以下失败(预期的):
Time: 127 ms, Memory: 12.00MB
There was 1 failure:
1) Tests\Unit\AccountApiTest::testGet
Invalid JSON was returned from the route.
我的问题是,对于Laravel中所有可用的断言,如果测试失败,我们有什么方法可以自定义生成的消息?在这种情况下,消息
Invalid JSON was returned from the route.
答案 0 :(得分:1)
否,您无法轻易地修改该消息,因为该消息已在Illuminate/Foundation/Testing/TestResponse.php中进行了硬编码:
/**
* Validate and return the decoded response JSON.
*
* @param string|null $key
* @return mixed
*/
public function decodeResponseJson($key = null)
{
$decodedResponse = json_decode($this->getContent(), true);
if (is_null($decodedResponse) || $decodedResponse === false) {
if ($this->exception) {
throw $this->exception;
} else {
PHPUnit::fail('Invalid JSON was returned from the route.');
}
}
return data_get($decodedResponse, $key);
}
您可以通过覆盖TestResponse来获取自定义消息。