php - Laravel 4 unittest using wrong database -
i have created testing database environment follows:
return array( 'default' => 'sqlite', 'connections' => array( 'sqlite' => array( 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '' ), ) ); when initialise database in setup function appears work fine:
mail::pretend(true); artisan::call('migrate'); $this->seed(); any query directly in test directly in test class returns expect if call function on model database uses 'live' (i'm running in vagrant dev server no risk of ruining anything) database instead of test one. need change configuration further ensure using test database? or need instantiate model in special way?
an example of doesn't work. in test:
// gets correct company $comp = companies::find(1); // gets results wrong database $comp->quotesandorders(); where quotesandorders simple query on hasmany relationship (orders)
$this->orders()->get();
first of on our .env file have add new variable. looks this.
db_connection=sqlite that's because of laravel defaults mysql when variable absent .env file:
see here..
'default' => env('db_connection', 'mysql') now our app pointing our sqlite database can run our migrations , seeding. after if want keep running mysql remove db_connection=sqlite .env file.
now on root folder have phpunit.xml file, open , new variable under <php> node
<env name="db_connection" value="sqlite"/>
Comments
Post a Comment