28

I'm sending a post request in a test case, and I want to assert that a specific element. Let's say I have a key x which exists in the response. In this case, I can't say seeJson(['x' => whatever]); because the value is unknown. For sure, I can't do it with seeJson(['x']);.

Is there a way to solve this?

If it matters:

Laravel: v5.2.31

PHPUnit: 5.3.4

7
  • Why you didn't try to dump response object? Commented May 16, 2016 at 12:34
  • @zhilevan: I didn't know I could :-) Commented May 16, 2016 at 12:36
  • dump() is a void method. If I could use it, i'd have to use ob_start() and ob_get_clean(). I'll have to check and see if I can use that in a test. Commented May 16, 2016 at 12:38
  • And one more thing. How would you pass or fail a test then? Because you'd have to break the test request's method chain. I found a fail method. But I'll have to look into it more. Commented May 16, 2016 at 12:40
  • Nope, don't worry about those. You can't get the dump in a variable using ob_start() in a test :-( Commented May 16, 2016 at 12:53

2 Answers 2

52

May it will be helpful for anyone else. You can write this test for your check response json structure

$this->post('/api/login/', [
        'email' => '[email protected]',
        'password' => '123123123',
    ])->assertJsonStructure([
        'status',
        'result' => [
            'id',
            'email',
            'full_name',
        ],
    ]);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, this works. Note when writing Laravel HTTP tests, it is $response->assertJsonStructure(array $structure);
How would you assert that a child array has X members, or possibly check that it has "no more than X members" or "no less than X members"?
Please edit your answer from seeJsonStructure to assertJsonStructure. I wasted time trying to get your answer to work, because that method name does not exist. I would edit the answer myself, but such an edit should be rejected as "not what the author intended".
@CJDennis Question is tagged with laravel-5.2, in that version it was seeJsonStructure.
13

Although it's not optimal at all, I chose to use this code to test the situation:

$this->post(URL, PARAMS)->see('x');

X is a hypothetical name, and the actual element key has a slim chance of popping up in the rest of the data. otherwise this nasty workaround wouldn't be practical.

UPDATE:

Here's the solution to do it properly:

public function testCaseName()

{
    $this->post(route('route.name'), [
        'param1' => 1,
        'param2' => 10,
    ], [
        'headers_if_any' => 'value'
    ]);

    $res_array = (array)json_decode($this->response->content());

    $this->assertArrayHasKey('x', $res_array);
}

3 Comments

padding true as the second parameter to json_decode will return the values as array json_decode($this->response->content(), true) php.net/json_decode
$this->response is not available any longer in modern Laravel. Current code would look like $r = $this->post(...); then you can work with $r->getContent()
Nowadays it is not appropriate for Laravel 11.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.