(FOUND)测试中的默认出厂值

时间:2017-01-13 00:01:29

标签: php laravel testing

我继承了一个Laravel 5.3 RESTful API项目,并且仍然围绕着它。在users表格中,我删除了name列,并将其替换为first_namelast_name。然而,这已经破坏了现有的测试。

当我运行phpunit时,我看到以下错误:

   Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1364    Field 'first_name' doesn't have a default value (SQL: insert into `users`  (`name`, `email`, `password` ...

我搜索过播种机,工厂文件和Google,但我找不到如何更新测试以反映这些变化。我能看到的唯一提示是错误是由测试$user = factory(\App\Models\User::class)->create();中的这一行创建的。但是我无法看到为测试配置字段的位置。

我看到人们建议编辑config / database.php并设置strict=false。然而,这听起来像是一个黑客,而不是解决问题。

任何方向都会非常感激。

编辑:没关系。我发现文件在一个非常明显的地方。 Database/factories/

1 个答案:

答案 0 :(得分:0)

不要直接删除数据库中的列或添加列。 您应该创建两个迁移:

php artisan make:migration drop_name_from_users_table

php artisan make:migration add_first_name_and_last_name_to_users_table

class DropNameFromUsersTable extends Migration
{
    public function up()
    {
        Schema::table('users', function ($table) {
            $table->dropColumn('name');
        });
    }

    public function down()
    {
        Schema::table('users', function ($table) {
            $table->string('name');
        });
    }
}

class AddFirstNameAndLastNameToUsersTable extends Migration
{
    public function up()
    {
        Schema::table('users', function ($table) {
            $table->string('first_name');
            $table->string('last_name');
        });
    }

    public function down()
    {
        Schema::table('users', function ($table) {
            $table->dropColumn('first_name');
            $table->dropColumn('last_name');
        });
    }
}
相关问题