如果你真的想测试一个关系方法,你可以做到这一点,甚至不需要将模型保存到数据库中。
您仍然需要使用 RefreshDatabase 特征(或 DatabaseMigrations 特征),否则模型将不会映射到任何表。
# tests/Unit/ParentTest.php
/**
* Test Parent has HasMany relationship with Child model
* @test
*/
public function has_many_children_with_parent_id_fk()
{
$parent = new Parent;
$foreign_key = 'parent_id';
// Get the relationship object, not the data collection
$relationship = $parent->children();
$related_model = $relationship->getRelated();
// Assert this is a HasMany relationship
$this->assertInstanceOf(HasMany::class, $relationship);
// Assert the related model is Child
$this->assertInstanceOf(Child::class, $related_model);
// Assert the foreign key is the one we specified
// (This can be useful if you do not use the default one)
$this->assertEquals($foreign_key, $relationship->getForeignKeyName());
// Assert the foreign key is a column
// of the database table mapped by the Child model
$this->assertTrue(Schema::hasColumns($related_model->getTable(), array($foreign_key)));
}
# tests/Unit/ChildTest.php
/**
* Test Child has belongsTo relationship with Parent model
* @test
*/
public function belongs_to_parent_with_parent_id_fk()
{
$child = new Child;
$foreign_key = 'parent_id';
// Get the relationship object, not the data collection
$relationship = $child->parent();
$related_model = $relationship->getRelated();
// Assert this is a BelongsTo relationship
$this->assertInstanceOf(BelongsTo::class, $relationship);
// Assert the related model is Parent
$this->assertInstanceOf(Parent::class, $related_model);
// Assert the foreign key is the one we specified
// (This can be useful if you do not use the default one)
$this->assertEquals($foreign_key, $relationship->getForeignKeyName());
// Assert the foreign key is a column
// of the database table mapped by the Child model
$this->assertTrue(Schema::hasColumns($relationship->getParent()->getTable(), array($foreign_key)));
}
这有点少,但您可以进行自定义断言以将所有这些都封装在所有测试文件扩展的 TestCase 中。以下方法适合我的需要
# tests/TestCase.php
public function assertHasManyUsing($related_model, $relationship, $foreign_key)
{
$this->assertInstanceOf(HasMany::class, $relationship);
$this->assertInstanceOf($related_model, $relationship->getRelated());
$this->assertEquals($foreign_key, $relationship->getForeignKeyName());
$this->assertTrue(Schema::hasColumns($relationship->getRelated()->getTable(), array($foreign_key)));
}
public function assertBelongsToUsing($related_model, $relationship, $foreign_key)
{
$this->assertInstanceOf(BelongsTo::class, $relationship);
$this->assertInstanceOf($related_model, $relationship->getRelated());
$this->assertEquals($foreign_key, $relationship->getForeignKeyName());
$this->assertTrue(Schema::hasColumns($relationship->getParent()->getTable(), array($foreign_key)));
}
现在,重构测试看起来像这样
# tests/Unit/ParentTest.php
/**
* Test Parent has HasMany relationship with Child model
* @test
*/
public function has_many_children_with_parent_id_fk()
{
$parent = new Parent;
$this->assertHasManyUsing(Child::class, $parent->children(), 'parent_id');
}
# tests/Unit/ChildTest.php
/**
* Test Child has belongsTo relationship with Parent model
* @test
*/
public function belongs_to_parent_with_parent_id_fk()
{
$child = new Child;
$this->assertBelongsToUsing(Parent::class, $child->parent(), 'parent_id');
}