php Laravel Eloquent,Assert集合包含项

mrphzbgm  于 2023-11-16  发布在  PHP
关注(0)|答案(2)|浏览(120)

如何Assert(在PHPUnit测试中)Eloquent集合包含一个项?
就像这样:

$expected = factory::create(Item::class)->create();
$eloquentCollection = someData(); // Item::orderBy(...)->...->get();
$this->assertContains($expected, $eloquentCollection);

字符串

nc1teljy

nc1teljy1#

您可以使用contains方法来assertTrue测试:

$this->assertTrue($eloquentCollection->contains($expected));

字符串
你也可以将一个键/值对传递给contains方法,它将确定给定的对是否存在于集合中:

$this->assertTrue($eloquentCollection->contains('id', $expected->id));

9wbgstp7

9wbgstp72#

在PHPUnit的更高版本(8.5,9.6,10.4)中,有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

相关问题