aspirinemaga
aspirinemaga

Reputation: 3947

Laravel TDD assertSee returns true on NULL object

Whenever I try to test some object property which does not exists (which is actually null), tests passes when using $this->assertSee($record->someNonExistingProperty):

public function test_a_user_should_be_able_to_see_a_consumer_address()
{
    $this->get(route('users.index'))->assertSee($this->user->address);
}

$this->user->address does not exists, but test returns green

What's wrong with that assertion ?

Upvotes: 0

Views: 860

Answers (2)

Coola
Coola

Reputation: 3162

A workaround for this is to use a null coalescing operator like:

$response ->assertSeeText($this->user->address ?? '**No Address Field**')

This part $this->user->address ?? '**No Address Field**' checks if there is a $this->user->address and if not returns **No Address Field** - as long as your view does not show this text it will fail.

So basically change the text of **No Address Field** to something you want to see in the test results to identify the missing key, but is not present in the view.

Upvotes: 0

Spholt
Spholt

Reputation: 4032

My guess would be this, it is asserting it can see nothing/everything.

If $this->user->address return null then your assertion would equate to this:

$this->get(route('users.index'))->assertSee(null);

I would also do an assertion on your objects property too. Like so:

$this->assertTrue($this->user->address !== null);
$this->get(route('users.index'))->assertSee($this->user->address);

The documentation on assert see states it looks for a string and will escape any value it is given

https://laravel.com/docs/7.x/http-tests#assert-see

Upvotes: 1

Related Questions