jeudi 10 novembre 2016

Laravel Test Not Validating Database Constraint

I have a Laravel 5.3 app that includes a users table. One of the fields on that table is api_token, which in the database definition is set to NOT NULL. If I use Postman and hit my API endpoint for creating a user and don't include the api_token field, I receive the following error: SQLSTATE[HY000]: General error: 1364 Field 'api_token' doesn't have a default value.

I haven't added code to the generate the token yet, so that's exactly what I'd expect. However, I have this PHPUnit test:

/** @test */
public function a_user_can_be_created()
{
    $this->user_data = [
        'first_name' => 'Jane',
        'last_name' => 'Smith',
        'email_address' => 'jsmith@mailinator.com',
        'phone' => '1234567890',
        'username' => 'jsmith',
        'password' => 'testing'
    ];

    $this->post('/api/users', $this->user_data, array(
                'Accept' => 'application/json'
            ))
         ->assertResponseStatus(201)
         ->seeJsonEquals(['status' => 'success'])
         ->seeInDatabase('users', $this->user_data);
}

The problem is that the test passes. How can this test be successful when the database should be throwing an exception when the user is saved?

I've 'died and dumped' at various points and confirmed that the data is in fact being saved to the database when the automated test runs. I've also tried using the same DB for both Postman and the automated test (instead of a separate testing DB) - same result.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire