jeudi 18 juillet 2019

Are unit tests superfulous if functional tests provide related code coverage

I have written some functional tests which assert that the responses I receive from an endpoint have the expected status codes and error messages based on the input validation they perform.

So for the classic example I have some rules for validating a name for an Account where the name is required:

Functional test

/** @test */
public function an_account_name_is_required()
{
    $this->authenticate();

    $data = factory(Account::class)->raw();
    unset($account['data']);

    $this->json('POST', route('accounts.create'), $data)
        ->assertJson([
            'status' => 422,
            'errors' => [
                'name' => ['The name field is required.']
            ]
        ])
        ->assertStatus(422);

    $this->assertDatabaseMissing('accounts', $data);
}

Unit test

/** @test */
public function it_is_invalid_if_the_name_is_missing()
{
    $account = factory(Account::class)->raw();
    unset($account['data']);

    $v = Validator::make($account, Account::rules());

    $this->assertTrue($v->fails());
}

In the given example above, my functional test which asserts that I get the correct response data and structure based on failed validation. I can therefore assume that the input is being validated correctly, therefore, is the unit test superfluous?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire