我有一个检查 api 的 json 响应的测试
public function test_search_get_place_details()
{
$this->post('/api/searchVenue', ['placeId' => 'ChIJXZE0JMLeekgR2meph0M9Jm41221212112']);
$this->SeeJson([
'Town' => 'Birkenhead',
]);
}
此测试有效并正确断言,但当它失败时,我得到的只是
Time: 1.42 seconds, Memory: 27.00MB
There was 1 error:
1) SearchTest::test_search_get_place_details
ErrorException: Invalid argument supplied for foreach()
/home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Support/Arr.php:494
/home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:232
/home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:257
/home/ubuntu/workspace/tests/SearchTest.php:41
我认为这是因为响应是典型的 Laravel 错误而不是 json。但我需要知道该错误是什么,因为它发生在我的 CI 机器上,而不是本地。
有没有办法让它在失败时输出响应包含的内容?
最佳答案
当测试框架没有看到我告诉它期望的内容时,它无法提供关于它看到的内容的任何体面指示时,我总是觉得很烦人。除了围绕断言包装一个 try...catch...report...rethrow 之外,我知道没有合适的方法让 MakesHttpRequests
特征仅在测试失败时吐出它看到的内容.
但是,要调试它,您可以继续这样做,并使用 $this->response
查看响应中的内容。
关于laravel - 无法从 SeeJson 单元测试中获取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38600422/