0

When creating a record in setUp():

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

  $company = new App\Company();
  $company->company_name = 'MyTest';
  $company->save();
}

I get the following error:

Base table or view not found

I am using use DatabaseMigrations; in my TestCase. It could be that the migrations run just before a testCase starts, so when `setUpz is run there is no table created yet.

Using Laravel 5.1

Currently I have to repeat myself by creating this record in every test.

Any ideas how to make this work?

tread
  • 10,133
  • 17
  • 95
  • 170
  • What version of Laravel are you using? Does the model work in context other than test? – Mark Davidson Dec 23 '15 at 09:57
  • I've updated the question (version 5.1), yes it works in all scenarios including in the test case tests, but not in the `setUp()` function – tread Dec 23 '15 at 10:05

2 Answers2

1

I had the same problem. Ended up using the DatabaseTransactions trait instead of DatabaseMigrations. That's not enough though, as you still need to run your migrations and create the db tables. You can do this by also overriding the setUp method on the main TestCase class:

public function setUp()
{
    parent::setUp();
    $this->artisan('migrate:refresh');
}

This way you rollback and rebuild all tables before each test. And your tests are wrapped in transactions, which are also rolled back.

You can then override the setUp function on your individual test classes and do DB stuff without errors.

kyrpas
  • 11
  • 1
0

Try adding a \ befor App, like this:

$company = new \App\Company();
smartrahat
  • 5,381
  • 6
  • 47
  • 68