简体   繁体   English

使用 PHPUnit 的 Lumen 测试显示太多断言,只有 2 个测试

[英]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?因为我意识到seeJsonStructure()正在调用许多断言取决于嵌套的数组结构,如果我的测试单元做出太多断言会在将来有很多测试文件时产生问题吗?

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.如果您查看 assertJsonStructure 的内容,您可以看到此函数是如何工作的,它基本上遍历所有键并针对响应进行断言。 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.所以你有 6 个断言,它们是由 assertJsonStructure 生成的,还有一个是由 seeStatusCode 生成的。 which it'll be a total of 7 assertions, you have two tests which sum of them is 14.总共有 7 个断言,您有两个测试,它们的总和为 14。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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