3

I have to admit that it's the first time I use this method

Before the description, here is the Laravel and PHP version in this case. Laravel: 8.15 PHP: 7.4.13

  • My Route:
Route::get('/', function () {
    return ['a' => 1, 'b' => 2, 'c' => 3];
  • When my testing method was as follows, it failed, it's what I expected
    public function testDemo()
    {
        $response = $this->get('/')->assertJsonMissing(['a' => 1, 'b' => 3]);
    }
  • And then, I used assertJsonMissingExact, and I expected it would pass.
    public function testDemo()
    {
        $response = $this->get('/')->assertJsonMissingExact(['a' => 1, 'b' => 3]);
    }
  • However, it didn't pass. Instead, here is the message
This test did not perform any assertions

Time: 00:00.178, Memory: 20.00 MB


OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Risky: 1.
Process finished with exit code 0

It says that the test didn't perform any assertion. I'm wondering if it's normal? Because I'm expecting a passed result.

Anyone could help me with that will be so much appreciated.

Ray
  • 333
  • 4
  • 11

1 Answers1

1

Might be too late for you, but maybe not for the next person (like me) who wanted to understand the two methods.

looking at the base implementations of the two

Illuminate/Testing/AssertableJsonString::assertMissing() In simplified code (left out sorting of arrays)

public function assertMissing(array $data, $exact = false)
{
    $actual = json_encode($actualData);

    foreach ($data as $key => $value) {
        if (Str::contains($actual, $unexpected){
           PHPUnit::Fail()
        }
    }

    return $this;
}

Illuminate/Testing/AssertableJsonString::assertMissingExact()

In simplified code

public function assertMissing(array $data, $exact = false)
{
    $actual = json_encode($actualData);

    foreach ($data as $key => $value) {
        if (! Str::contains($actual, $unexpected){
           return $this;
        }
    }

    PHPUnit::Fail()
}

Therefore, assertJsonMissing fails if the whole array is in the actual data, while assertJsonMissingExact fails if at least one key => value pair is in the actual data, in your case 'a' => 1

Sofia
  • 11
  • 1