【问题标题】:Laravel 5.6. How to test JSON/JSONb columns拉拉维尔 5.6。如何测试 JSON/JSONb 列
【发布时间】:2018-10-16 14:26:23
【问题描述】:

$this->assertDatabaseHas() 不适用于 JSON/JSONb 列。

那么如何在 Laravel 中测试这些类型的列呢?

目前,我有一个商店行动。如何执行断言,即保存了具有预定义值的特定列。

类似

['options->language', 'en']

不是选项,因为我有一个包含元数据的广泛 JSON。

如何一次查看数据库中的JSON

【问题讨论】:

    标签: laravel postgresql jsonb laravel-5.6 laravel-testing


    【解决方案1】:

    Laravel 7+

    不确定这个解决方案可以追溯到多远。

    我找到了解决方案。忽略一些数据标签,一切都可以访问,我只是在玩弄我的测试来弄清楚。

    /**
     * @test
     */
    public function canUpdate()
    {
        $authUser = UserFactory::createDefault();
        $this->actingAs($authUser);
    
        $generator = GeneratorFactory::createDefault();
    
        $request = [
            'json_field_one' => [
                'array-data',
                ['more-data' => 'cool'],
                'data' => 'some-data',
                'collection' => [
                    ['key' => 'value'],
                    'data' => 'some-more-data'
                ],
            ],
            'json_field_two' => [],
        ];
    
        $response = $this->putJson("/api/generators/{$generator->id}", $request);
        $response->assertOk();
    
        $this->assertDatabaseHas('generators', [
            'id' => $generator->id,
            'generator_set_id' => $generator->generatorSet->id,
    
            // Testing for json requires arrows for accessing the data
            // For Collection data, you should use numbers to access the indexes
            // Note:  Mysql dose not guarantee array order if i recall. Dont quote me on that but i'm pretty sure i read that somewhere.  But for testing this works
            'json_field_one->0' => 'array-data',
            'json_field_one->1->more-data' => 'cool',
    
            // to access properties just arrow over to the property name
            'json_field_one->data' => 'some-data',
            'json_field_one->collection->data' => 'some-more-data',
    
            // Nested Collection
            'json_field_one->collection->0->key' => 'value',
    
            // Janky way to test for empty array
            // Not really testing for empty
            // only that the 0 index is not set
            'json_field_two->0' => null,
        ]);
    }
    

    【讨论】:

    • 嗯...如果这可行,那就太棒了!但是,请清理你的答案。真的很值得发布,只是......你能让其他开发者更方便吗?))
    • 该死,我需要这个'prompters->collectionA->0->not-working' => 'cant-access', 才能工作。
    • 它起作用了,我更新了单词所以它不会混淆,那是我在玩,当它起作用时,我只是粘贴了结果。我的错。 @ChristhoferNatalius
    • @ShawnPivonka 对我不起作用,这就是我评论的原因。我知道为什么。我改用 assertEquals。
    • @ChristhoferNatalius 我认为问题是我使用的是 Laravel 7。我猜 5.6 这可能行不通。你使用的是什么版本的 Laravel?
    【解决方案2】:

    更新

    现在可以完成like that


    我已经用这个单线解决了它(根据你的模型/字段调整它)

    $this->assertEquals($store->settings, Store::find($store->id)->settings);

    【讨论】:

      【解决方案3】:

      在对我有用的值上使用json_encode

      $this->assertDatabaseHas('users', [
          'name' => 'Gaurav',
          'attributes' => json_encode([
              'gender' => 'Male',
              'nationality' => 'Indian',
          ]),
      ]);
      

      【讨论】:

        猜你喜欢
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 2019-01-01
        • 2018-06-08
        • 2020-05-08
        • 2018-11-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多