如何Assert(在PHPUnit测试中)Eloquent集合包含一个项?就像这样:
$expected = factory::create(Item::class)->create(); $eloquentCollection = someData(); // Item::orderBy(...)->...->get(); $this->assertContains($expected, $eloquentCollection);
字符串
nc1teljy1#
您可以使用contains方法来assertTrue测试:
contains
assertTrue
$this->assertTrue($eloquentCollection->contains($expected));
字符串你也可以将一个键/值对传递给contains方法,它将确定给定的对是否存在于集合中:
$this->assertTrue($eloquentCollection->contains('id', $expected->id));
型
9wbgstp72#
在PHPUnit的更高版本(8.5,9.6,10.4)中,有assertContains。
assertContains
$needle = 'a'; $haystack = ['a', 'b', 'c']; $this->assertContains($needle, $haystack);
字符串https://docs.phpunit.de/en/8.5/assertions.html#assertcontainshttps://docs.phpunit.de/en/9.6/assertions.html#assertcontainshttps://docs.phpunit.de/en/10.4/assertions.html#assertcontains
2条答案
按热度按时间nc1teljy1#
您可以使用
contains
方法来assertTrue
测试:字符串
你也可以将一个键/值对传递给contains方法,它将确定给定的对是否存在于集合中:
型
9wbgstp72#
在PHPUnit的更高版本(8.5,9.6,10.4)中,有
assertContains
。字符串
https://docs.phpunit.de/en/8.5/assertions.html#assertcontainshttps://docs.phpunit.de/en/9.6/assertions.html#assertcontainshttps://docs.phpunit.de/en/10.4/assertions.html#assertcontains