D.R*_*.R. 5 postgresql laravel jsonb laravel-testing laravel-5.6
$this->assertDatabaseHas()
不使用JSON
/JSONb
列。
那么如何在 Laravel 中测试这些类型的列呢?
目前,我有一个商店行动。如何执行断言,保存具有预定义值的特定列。
就像是
['options->language', 'en']
不是一个选项,因为我有一个包含元内容的大量 JSON 。
如何JSON
立即检查数据库中的内容?
不确定这个解决方案可以追溯到多久之前。
我找到了解决办法。忽略一些数据标签,一切都是可访问的,我只是通过测试来弄清楚。
/**
* @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,
]);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4195 次 |
最近记录: |