Reputation: 644
Each time I run my tests with
php artisan test
after when I want to see the inserted data in my database, the table is totally empty.
Here is my test
class ExampleTest extends TestCase
{
use RefreshDatabase;
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$ad = new Ad();
$ad->name = 'test';
$ad->price = 100000;
$ad->save();
$this->assertTrue($ad->id > 0);
var_dump($ad->id);
}
}
The test verifies if the database has well generated an id, and the result is correct each time.
From my point of view, the trait RefreshDatabase refreshes the database BEFORE each tests not after so .. how in the world is this possible?
NB: I am sure that I am checking the correct database because if I remove use RefreshDatabase
I can see the records.
Precision:
If instead of RefreshDatabase
I use DatabaseTransactions
I can see the records after running tests. I can't get my head around it...
Upvotes: 5
Views: 4353
Reputation: 1656
Add this to your phpunit.xml
file
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
Something like this:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>app</directory>
</include>
</source>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
</phpunit>
Upvotes: 3
Reputation: 123
I am assuming you are using RefreshDatabase
already.
The first thing to check is your phpunit.xml file and its DB settings. It should be an in-memory database.
The second thing to check you are not caching the configs on the dev environment.
First run php artisan config:clear
only once. Then run your tests.
Note: do not cache the config for local development.
Upvotes: 7