tho*_*dic 7 laravel laravel-validation laravel-testing
我有以下测试:
public function testStoreMemberValidation()
{
$response = $this->withExceptionHandling()->post('/api/members', [
"name" => "Eve",
"age" => "invalid"
], ['X-Requested-With' => 'XMLHttpRequest']);
dd($response->json());
};
Run Code Online (Sandbox Code Playgroud)
我试图断言响应是验证错误的形式。控制器方法如下:
public function store(Request $request)
{
$data = $request->validate([
'name' => 'required|string',
'age' => 'required|integer',
]);
Member::create($data);
}
Run Code Online (Sandbox Code Playgroud)
但是,每当我调用任何调用$response->json()(大多数断言)的断言时,都会出现异常:
Illuminate\Validation\ValidationException :给定的数据无效。
如何在不抛出此错误的情况下对此响应执行断言?
请注意,我使用的是 Laravel 5.7。
Mic*_*yen 10
你有withExceptionHandling()你的测试,删除它,它应该可以工作。
$response = $this->withExceptionHandling()->post('/api/members', [
"name" => "Eve",
"age" => "invalid"
], ['X-Requested-With' => 'XMLHttpRequest']);
Run Code Online (Sandbox Code Playgroud)
应该
$response = $this->post('/api/members', [
"name" => "Eve",
"age" => "invalid"
], ['X-Requested-With' => 'XMLHttpRequest']);
Run Code Online (Sandbox Code Playgroud)