如何为json响应编写测试?

时间:2017-05-10 08:22:52

标签: php json unit-testing laravel-5 laravel-5.1

我有函数返回JSON

return response()->json([
            'status' => 'OK',
            'data' => [
                'id' => $routeForecast->id,
            ],
        ])

我怎么能对此进行测试..我的laravel版本是5.1,我使用assertJson([])它给我这个错误

***** PHPUnit_Framework_Exception: Argument #1 (No Value) of PHPUnit_Framework_Assert::assertJson() must be a string

并尝试seeJson它给我这个错误

***** Invalid JSON was returned from the route. Perhaps an exception was thrown?

我该如何解决?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情。

    // When
    $response = $this->getJson( 'v1/helloWorld' );

    //dd( $response ); // perhaps die and dump here to check response?

    // Then
    $response
        ->assertStatus( 200 )
        ->assertJson( [
            'status' => 'OK',
        ] );