0

我正在尝试验证功能测试。这是我的控制器:

$attributes = $request->validate([
    'name' => 'required',
    'description' => 'required',
    'address' => 'required',
    'city' => 'required',
    'state' => 'required|max:2',
    'zip' => 'required|min:5',
]);

Location::create($attributes);

return redirect('/locations', 301);

我是说state最多只能有 2 个字符。

我的测试如下所示:

$this->withExceptionHandling();
$user = factory(User::class)->create();
$location = factory(Location::class)->raw(['state' => 'qwerty']);

$response = $this->actingAs($user)->post('/locations', $location);


$response->assertStatus(302);
$response->assertSessionHasErrors([
    'state' => 'The state may not be greater than 2 characters.'
]);

并且该测试将通过。

但是,如果我将其更改为:

...
$location = factory(Location::class)->raw(['state' => 'AZ']);
....

预期状态代码 302,但收到 301。未能断言 false 为 true。

它会失败。我很困惑,因为我想要一个 2 字符状态。任何建议都非常感谢!

编辑

我还尝试将该messages方法添加到我的控制器中,以准确返回该错误消息,但没有成功。

public function messages()
{
    return [
        'state.max' => 'The state may not be greater than 2 characters.',
    ];
}
4

0 回答 0