Answer the question
In order to leave comments, you need to log in
Why does it display an error when creating a test to check the length of a name?
Displays an error
Session missing key: errors
│ Failed asserting that false is true.
class MyUserTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->user = factory(User::class)->make();
}
/** @test */
function name_should_not_be_too_long22()
{
$response = $this->post('/users', [
'name' => 'sdsfaf',
'email' => '[email protected]',
'password' => 'secret',
]);
$response->assertSessionHasErrors([
'name' => 'The name may not be greater than 50 characters.'
]);
}
}
class MyUsersController extends Controller
{
public function store(Request $request)
{
$data = $request->validate([
'name' => 'required|max:50',
'email' => 'required|email|max:255',
'password' => 'required',
]);
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
Answer the question
In order to leave comments, you need to log in
This is how it should work
function name_should_not_be_too_long22()
{
$response = $this->post('/users', [
'name' => 'qwertgfdsazxcvbgtrewqasdfgbvcxzasdfgtrewqasdfgbvcxza',
'email' => '[email protected]',
'password' => 'secret',
]);
$response->assertSessionHasErrors([
'name' => 'The name may not be greater than 50 characters.'
]);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question