D
D
Div-Man2018-12-27 10:02:47
Laravel
Div-Man, 2018-12-27 10:02:47

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

1 answer(s)
A
Alexander Kovalchuk, 2018-12-27
@Div-Man

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 question

Ask a Question

731 491 924 answers to any question