首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Laravel和Pest测试多维阵列

用Laravel和Pest测试多维阵列
EN

Stack Overflow用户
提问于 2022-05-14 22:47:56
回答 2查看 413关注 0票数 3

当我想在单元测试中测试数组时,我遇到了一个小问题。

我想测试键的结构和类型,但是我不知道如何处理它(我试过了,我保证!)

以下是json输入:

代码语言:javascript
复制
{
    "success": true,
    "data": [
        {
            "id": 1,
            "domains_id": 1,
            "sub": "",
            "type": "",
            "ip_or_fqdn": "",
            "created_at": "2022-05-14T08:30:18.000000Z",
            "updated_at": "2022-05-14T08:30:18.000000Z"
        }
    ],
    "message": "Domain retrieved successfully."
}

目前,有一项测试是:

代码语言:javascript
复制
it('fetch zone entries [GET] with json response and check response type', function () {

    TestCase::initDatabase();

    Passport::actingAs(
        User::factory()->make()
    );

    $response = $this->withHeaders([
            'Accept' => 'application/json'
        ])
        ->json('GET', '/api/zone')
        ->assertStatus(200)
        ->assertJson(function (AssertableJson $json) {
            $json->has('success')
                 ->whereType('success', 'boolean')
                 ->has('data')
                 ->whereType('data', 'array')
                 ->has('message')
                 ->whereType('message', 'string');
    });

    TestCase::resetDatabase();
});

我想用这个过程来测试“数据”数组键/值,当然,在这个闭包中也是如此;但是它有可能吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-05-15 08:16:08

例如,可以使用点表示法。

代码语言:javascript
复制
->assertJson(fn (AssertableJson $json) =>
    $json->has('data.id')
        ->where('data.id', 1)
        ->missing('data.x')
    );
票数 2
EN

Stack Overflow用户

发布于 2022-05-15 17:56:12

最后,使用@ajthinking技巧,这是最后的测试,它是有效的,非常感谢!

代码语言:javascript
复制
it('fetch zone entries [GET] with json response and check response type', function () {

    TestCase::initDatabase();

    Passport::actingAs(
        User::factory()->make()
    );

    $response = $this->withHeaders([
        'Accept' => 'application/json'
    ])
        ->json('GET', '/api/zone')
        ->assertStatus(200)
        ->assertJson(function (AssertableJson $json) {
            $json->has('success')
                 ->whereType('success', 'boolean')
                 ->has('data')
                 ->whereType('data', 'array')
                 ->has('data.0')
                 ->has('data.0')
                    ->has('data.0.id')
                    ->has('data.0.sub')
                    ->has('data.0.type')
                    ->has('data.0.ip_or_fqdn')
                    ->has('data.0.created_at')
                    ->has('data.0.updated_at')
                    ->whereType('data.0.id', 'integer')
                    ->whereType('data.0.sub', 'string')
                    ->whereType('data.0.type', 'string')
                    ->whereType('data.0.ip_or_fqdn', 'string')
                    ->whereType('data.0.created_at', 'string')
                    ->whereType('data.0.updated_at', 'string')
                 ->has('message')
                 ->whereType('message', 'string');
    });

    TestCase::resetDatabase();
});

将来,我将使用assertJsonStructure()来改进这个测试,以便在assertJson()闭包中测试基本结构和测试类型。

编辑:

下面是使用assertJsonStructure()方法进行的测试,它运行得很好:

代码语言:javascript
复制
it('fetch zone entries [GET] with json response and check response type', function () {

    TestCase::initDatabase();

    Passport::actingAs(
        User::factory()->make()
    );

    $response = $this->withHeaders([
        'Accept' => 'application/json'
    ])
        ->json('GET', '/api/zone')
        ->assertStatus(200)
        ->assertJsonStructure([
            'success',
            'data' => [
                '*' => [
                    'id',
                    'sub',
                    'type',
                    'ip_or_fqdn',
                    'created_at',
                    'updated_at'
                ]
                ],
            'message'
        ])
        ->assertJson(function (AssertableJson $json) {
            $json->whereType('success', 'boolean')
                 ->whereType('data', 'array')
                 ->whereType('data.0.id', 'integer')
                 ->whereType('data.0.sub', 'string')
                 ->whereType('data.0.type', 'string')
                 ->whereType('data.0.ip_or_fqdn', 'string')
                 ->whereType('data.0.created_at', 'string')
                 ->whereType('data.0.updated_at', 'string')
                 ->whereType('message', 'string');
    });

    TestCase::resetDatabase();
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72244362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档