I have a test file
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Faker\Factory as Faker;
class AdsTest extends TestCase
{
use DatabaseMigrations;
public function test_it_one()
{
$ad = factory(App\Ad::class, 2)
->create( [
'firstname' => 'jeremie',
'lastname' => 'ges'
]);
}
public function test_it_two()
{
foreach (App\Ad::get() as $value)
{
var_dump($value->slug);
}
}
}
I want to rollback after each test my database to avoid false positives and false negatives. So I used the trait DatabaseMigrations
, but it doesn't work, the method test_it_two
outputs that:
[15:13:56] PHPUnit 4.8.18 by Sebastian Bergmann and contributors.
Starting test 'AdsTest::test_it_one'.
.
Starting test 'AdsTest::test_it_two'.
.string(11) "jeremie-ges"
string(9) "test-test"
string(11) "test-test-2"
string(13) "ergerg-regerg"
string(11) "test-test-3"
string(13) "jeremie-ges-2"
string(10) "rgreg-regr"
string(13) "jeremie-ges-3"
string(13) "jeremie-ges-4"
string(13) "jeremie-ges-5"
string(13) "jeremie-ges-6"
string(13) "jeremie-ges-7"
string(13) "jeremie-ges-8"
string(13) "jeremie-ges-9"
string(14) "jeremie-ges-10"
string(14) "jeremie-ges-11"
string(14) "jeremie-ges-12"
string(14) "jeremie-ges-13"
string(14) "jeremie-ges-14"
What am I doing wrong ?
Solved: Before to run your tests, take care to have a clean database, $ artisan migrate:refresh does the trick ;)