Skip to content

Commit 0549cba

Browse files
Add descriptive error messages to assertViewHas() (#55392)
* Add descriptive error messages to assertViewHas() * Fix inconsistency and add missing dot * Add message to key-value case, move $actual for reuse, adjust messages. * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 8e2f48e commit 0549cba

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/Illuminate/Testing/TestResponse.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,21 +1192,21 @@ public function assertViewHas($key, $value = null)
11921192

11931193
$this->ensureResponseHasView();
11941194

1195+
$actual = Arr::get($this->original->gatherData(), $key);
1196+
11951197
if (is_null($value)) {
1196-
PHPUnit::withResponse($this)->assertTrue(Arr::has($this->original->gatherData(), $key));
1198+
PHPUnit::withResponse($this)->assertTrue(Arr::has($this->original->gatherData(), $key), "Failed asserting that the data contains the key [{$key}].");
11971199
} elseif ($value instanceof Closure) {
1198-
PHPUnit::withResponse($this)->assertTrue($value(Arr::get($this->original->gatherData(), $key)));
1200+
PHPUnit::withResponse($this)->assertTrue($value($actual), "Failed asserting that the value at [{$key}] fulfills the expectations defined by the closure.");
11991201
} elseif ($value instanceof Model) {
1200-
PHPUnit::withResponse($this)->assertTrue($value->is(Arr::get($this->original->gatherData(), $key)));
1202+
PHPUnit::withResponse($this)->assertTrue($value->is($actual), "Failed asserting that the model at [{$key}] matches the given model.");
12011203
} elseif ($value instanceof EloquentCollection) {
1202-
$actual = Arr::get($this->original->gatherData(), $key);
1203-
12041204
PHPUnit::withResponse($this)->assertInstanceOf(EloquentCollection::class, $actual);
12051205
PHPUnit::withResponse($this)->assertSameSize($value, $actual);
12061206

1207-
$value->each(fn ($item, $index) => PHPUnit::withResponse($this)->assertTrue($actual->get($index)->is($item)));
1207+
$value->each(fn ($item, $index) => PHPUnit::withResponse($this)->assertTrue($actual->get($index)->is($item), "Failed asserting that the collection at [{$key}.[{$index}]]' matches the given collection."));
12081208
} else {
1209-
PHPUnit::withResponse($this)->assertEquals($value, Arr::get($this->original->gatherData(), $key));
1209+
PHPUnit::withResponse($this)->assertEquals($value, $actual, "Failed asserting that [{$key}] matches the expected value.");
12101210
}
12111211

12121212
return $this;

0 commit comments

Comments
 (0)