Shandur
Shandur

Reputation: 41

Migrations in Laravel don't work properly while testing

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!

Upvotes: 1

Views: 894

Answers (1)

Shandur
Shandur

Reputation: 41

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();

}

Upvotes: 1

Related Questions