dozc1071
dozc1071
采纳率100%
2017-04-27 12:19

Laravel中的迁移在测试时无法正常工作

已采纳

I have an issue with running migrations while making a test. I have migrations in different places. User migrations depend on Company migrations but each time I run tests I have an error that table 'companies' doesn't exist.

Code from test class:

protected function setUp()
{
    parent::setUp();

    $this->artisan('migrate', [
        '--path' => ['Modules/Company/Database/Migrations', 
                            'Modules/User/Database/Migrations'],
    ]);
}

protected function tearDown()
{
    $this->artisan('migrate:reset', [
        '--path' => ['Modules/User/Database/Migrations', 
                           'Modules/Company/Database/Migrations'],
    ]);

    parent::tearDown();

}

Can anyone help me, please. Thanks!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dougan6982 dougan6982 4年前

    Problem was in two places:

    • 1) option --path was provided as an array(but no warnings are displayed);
    • 2) command migrate:reset(it resets ALL the migrations using provided --path so the error 'Undefined index: create_company_table' happens).

    Final version.

    protected function setUp()
    {
       parent::setUp();
       $this->artisan('migrate', [
             '--path' => 'Modules/Company/Database/Migrations',
       ]);
       $this->artisan('migrate', [
             '--path' => 'Modules/User/Database/Migrations',
       ]);
    }
    
    protected function tearDown()
    {
        $this->artisan('migrate:rollback', [
            '--path' => 'Modules/User/Database/Migrations/',
        ]);
        $this->artisan('migrate:rollback', [
            '--path' => 'Modules/Company/Database/Migrations/',
        ]);
    
        parent::tearDown();
    
    }
    
    点赞 评论 复制链接分享

相关推荐