Hiroki
Hiroki

Reputation: 4163

Laravel 5.2: Inverse (reverse) of seeJson in testing

I've been using PHPUnit 4.8 on Laravel 5.2, wondering if it's possible to see if an API call does NOT have a JSON object in its response.

You can see if the response has a particular object, but how about doing the opposite?

$this->json('GET', 'api/items')
        ->seeJson(['id' => "100"])
        ->notSeeJson(['id' => "222"])//Is there anything like it?
        ->assertResponseOk();

I've been reading the documentation of PHPUnit and Laravel 5.2, but haven't found how to achieve it.

Any advice will be appreciated.

PS

In order to make sure that a particular object isn't included in the response, it'll suffice to count the total number of objects the response has.

With newer versions of PHPUnit, it's possible to do so with assertJsonCount(2, 'data').

But how about PHPUnit4/Laravel5.2?

Upvotes: 0

Views: 325

Answers (1)

Devon Bessemer
Devon Bessemer

Reputation: 35337

It's important to note these are Laravel 5.2 methods, not PHPUnit methods, defined in Illuminate\Foundation\Testing\TestCase.

The inverse of seeJson is dontSeeJson.

$this->json('GET', 'api/items')
    ->seeJson(['id' => "100"])
    ->dontSeeJson(['id' => "222"])
    ->assertResponseOk();

Upvotes: 1

Related Questions