Laravel 雄辩,断言集合包含项目
如何断言(在PHPUnit测试中)Eloquent集合包含一个项目?
像这样:
$expected = factory::create(Item::class)->create();
$eloquentCollection = someData(); // Item::orderBy(...)->...->get();
$this->assertContains($expected, $eloquentCollection);
如何断言(在PHPUnit测试中)Eloquent集合包含一个项目?
像这样:
$expected = factory::create(Item::class)->create();
$eloquentCollection = someData(); // Item::orderBy(...)->...->get();
$this->assertContains($expected, $eloquentCollection);
您可以使用 contains
方法进行测试,如下所示:assertTrue
$this->assertTrue($eloquentCollection->contains($expected));
您还可以将键/值对传递给 contains 方法,该方法将确定集合中是否存在给定的对:
$this->assertTrue($eloquentCollection->contains('id', $expected->id));