php - laravel模型中关系的单元测试

标签 php unit-testing laravel phpunit constraints

我是 laravel 模型单元测试的新手。所以请看看并告诉我我做错了什么。我的代码在下面给出。我有 2 个模型 User 和 UserState。

模型用户

public function state()
    {
        return $this->hasOne('UserState');
    }

模型用户状态

public function user()
{
    return $this->belongsTo('User');
}

现在我正在为 UserState 编写单元测试。如下所示:

单元测试用户状态模型测试

public function testUserRelationIsTrue(){
    $user = new User();
    $user->username = 'testusername';
    $user->save();
    $this->assertEquals($user->user_id, $user->state->id);
}

在 phpunit 测试运行期间它产生错误

Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation:
1452 Cannot add or update a child row: a foreign key constraint fails

最佳答案

如果您真的想测试一个关系方法,您可以完成它,甚至不需要将模型保存到数据库中。 您仍然需要使用 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');
}

关于php - laravel模型中关系的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24991217/

相关文章:

unit-testing - 您如何对具有复杂输入输出的方法进行单元测试

java - 如果单例类本身正在使用 PowerMock 进行测试,如何模拟该类的私有(private)方法?

laravel - 为什么 _token 和 XSRF-TOKEN 在 Laravel 中不同?

php - 在 SPL 自动加载器中抛出异常?

PHP读取对象属性

php - 如何使用 ffmpeg 转换高质量的 .wmv 文件

javascript - Vue.js (laravel 6) 的值未显示并在控制台中出现错误

PHP 脚本似乎没有获取变量值

python - 如何修补日期时间的 now 方法,同时保留其他方法?

php - 无法使用 Laravel 5.1 将 user_context 传递到 Sentry