简体   繁体   中英

Lumen testing with PHPUnit showing too much assertions with only 2 tests

My test is working fine, but when I read this article , it's mention

One Assert Per Test Method

and I think my test is making too much assertions, here is my testing code:

<?php

use Laravel\Lumen\Testing\DatabaseMigrations;
use Laravel\Lumen\Testing\DatabaseTransactions;

class GuestTest extends TestCase
{
    use DatabaseTransactions;

    /** @test */
    public function guest_can_register()
    {
        $response = $this->json('POST', '/register', [
            "name" => "Khrisna Gunanasurya",
            "email" => "khrisnagunanasurya@gmail.com",
            "password" => "adminadmin",
            "password_confirmation" => "adminadmin"
        ]);

        $response
                ->seeStatusCode(HttpStatus::$CREATED)
                ->seeJsonStructure([
                    "data" => [
                        "type",
                        "id",
                        "attributes" => [
                            'name',
                            'email'
                        ]
                    ]
                ]);
    }

    /** @test */
    public function registered_guest_can_login()
    {
        $this->post('/register', [
            "name" => "Khrisna Gunanasurya",
            "email" => "khrisnagunanasurya@gmail.com",
            "password" => "adminadmin",
            "password_confirmation" => "adminadmin"
        ]);

        $response = $this->json('POST', '/login', [
            'email' => 'khrisnagunanasurya@gmail.com',
            'password' => 'adminadmin'
        ]);

        $response
                ->seeStatusCode(HttpStatus::$OK)
                ->seeJsonStructure([
                    'data' => [
                        'type',
                        'attributes' => [
                            'token',
                            'token_type',
                            'expires_in'
                        ]
                    ]
                ]);
    }
}

So what I want to ask, is my testing return too much assertions for those simple test? as I realize that seeJsonStructure() is calling many assertions depends on the nested array structures, and if my test unit makes too much assertions will creates a problem in the future when there are lots of test files?

EDIT

在此处输入图片说明

If you take a look at the contents of the assertJsonStructure you can see how this function works, It basically iterates over all keys and asserts against the response. so in this case

public function assertJsonStructure(array $structure = null, $responseData = null){
    foreach ($structure as $key => $value) {
        if (is_array($value)) {
            PHPUnit::assertArrayHasKey($key, $responseData);
            $this->assertJsonStructure($structure[$key], $responseData[$key]);
        } else {
            PHPUnit::assertArrayHasKey($value, $responseData);
        }
    }
}

So you have 6 assertions which are generated by the assertJsonStructure and one more by the seeStatusCode. which it'll be a total of 7 assertions, you have two tests which sum of them is 14.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM