LARAVEL 使用功能测试 phpunit 投射列 JSON

LARAVEL cast colums JSON with feature test phpunit

没有通过我最后一次比较刚刚输入的字段和发送的字段的测试,因为 json

我有以下字段:

public function up()
        {
            Schema::create('polls', function (Blueprint $table) {
                $table->id();
                $table->text('now');
                $table->json('paramJson');
                $table->enum('status', ['a', 'b', 'c','d'])->default('a');
            });
        }

投票模型:

protected $fillable=[
                'now',
           'paramJson',
            ];

//to cast that column from JSON to an array automatically (maybe it doesn't work)
protected $casts = [
                'paramJson' => 'array',
            ];

PollsController.php

public function add(Request $request)
    {
        $poll = Poll::create($request->all());
        return response()->json($poll, 201);
    }

Feature/PollApiTest.php

    $data = [
       "now" => "Will Messi sign for City?",
       "parameters" : {"a"=> "yes", "b"=> "no"},
    ];

    $response = $this->post('/api/polls',$data);//add data in polls

    $response ->assertStatus(201);// assert Ok

    $response->assertJson($data);//assert Ok

    $response = $this->get('/api/poll');//get index polls

$this->assertSame($dataDbTest[0]['paramJson'],$data['paramJson']);//this test fails

最后一次测试失败,因为低于错误

Failed asserting that Array &0 ( 'a' => 'yes' 'b' => 'no' ) is identical to '{"a": "yes", "b": "no"}'.

如果最后一个测试我用 json_encode:

$this->assertSame($dataDbTest[0]['paramJson'],json_encode($data['paramJson']));

--- Expected
+++ Actual @@ @@
-'{"a": "yes", "b": "no"}'
+'{"a":"yes","b":"no"}'

我觉得应该是这样的

$this->assertSame(json_decode($dataDbTest[0]['paramJson']),$data['paramJson']);

json_decode