I’m using Laravel with Pest. I have a file contains three test and out of which only one test involves db operation so I want to make sure db is only refreshed for that one test. So, I don’t want to use uses(RefreshDatabase::class);
on the top file file. What I did is:
//uses(RefreshDatabase::class);
it('test 1', function () {
//no db operations here
});
it('test 2', function () {
//no db operations here
});
it('successfully stores social connector', function () {
$repo->storeSocialConnector(["platform"=>"wix"]); //suppose it successfully stores record in social_connectors table
$this->assertDatabaseHas('social_connectors', [
'platform' => 'fooBar',
]);
})->uses(RefreshDatabase::class);
but the above code don’t refreshed the database when I run third test.
How I confirm this?
suppose I run this test two times likes
./vendor/bin/pest tests/Feature/Http/SomeTest.php --filter 'successfully stores social connector'
I got below output:
FAILED TestsFeatureHttpSomeTest > it successfully stores social connector
Failed asserting that a row in the table [social_connectors] matches the attributes {
"platform": "fooBar"
}.
Found: [
{
"platform": "wix",
},
{
"platform": "wix",
}
].
See, when I run the test second time it shows me it found two records in table, however it should say me it found just one record not two.
It means when first tim test failed the record is created and that record didn’t got cleared when I run the test second time. However db should be cleared after first try fail.
How to make sure db should be cleared after test executed so that when I execute test second time the data from last run don’t show up?