bennigraf's avatar

Lumen: using "seeJson()" reports "This test did not perform any assertions"

Hey all,

I'm testing an API with the test infrastructure provided by lumen. The test is basically something like:

$this->json('POST', 'api', [
    'query' => $this->query,
    'variables' => $some_variables
])->seeJson([
    'my' => 'expected_json_structure'
]);

And it does run and work as it fails when the expected Structure is different, but if it's actually successful, PHPUnit reports the test as Risky as "This test did not perform any assertions".

I did track it down to Laravel\Lumen\Testing\Concerns\MakesHttpRequests:seeJsonContains() (line 287), where the actual assertion is being called dynamically with call_user_func(['PHPUnit_Framework_Assert', $method], ...). If I change that to a direct call of PHPUnit::assertTrue, PHPUnit doesn't complain.

I don't know If this is a bug in lumen or a shortcoming of PHPUnit or if I'm writing my test wrong – does anyone have a suggestion on how to fix/improve this?

(I'm using Lumen 5.4 and PHPUnit 6.2.3 on PHP 7.1.5)

Best, Benjamin.

0 likes
1 reply
salmon's avatar

I think you need [] brackets on your example:

'my' => ['expected_json_structure']

Here is my working example.

    public function testToSeeIfItChecksForPasswordField()
    {
        $this->json('POST', '/auth/login', ['email' => '[email protected]'])
            ->seeJson([
                "password" => ["The password field is required."],
            ]);
    }

You might also use seeJsonStructure as below

    public function testLoginWithValidUser()
    {
        factory(User::class)->create(['first_name' => 'testFirstNameString', 'email' => 'user1@example.com', 'password' => app('hash')->make('123456')]);

        $this->json('POST', '/auth/login', ['email' => 'user1@example.com', 'password' => '123456'])
            ->seeJsonStructure(['token']);
    }

Please or to participate in this conversation.