This is my test:
use RefreshDatabase;
/** @test */
public function an_operator_can_view_changes(){
$this->withoutExceptionHandling();
$user = factory('App\User')->create();
$role = Role::firstOrCreate(['name' => 'Operator']);
$permission = Permission::firstOrCreate(['name' => 'view_changes']);
$role->givePermissionTo($permission);
$user->assignRole($role);
$this->actingAs($user);
$this->assertTrue($user->can('view_changes'));
}
if i launch it with Refreshdatabase it will fail from the second execution till i remove use Refreshdatabase
What's happening? Can't debug because if i add Refreshdatabase DB will be erased, as you see i already tried to get a fresh version of the user with no result.
I'm using firstOrCreate because default records will be populated with migration so view_changes and operator already exists in DB.
Test with Refreshdatabase:
root@918e53969b70:/var/www# phpunit --filter an_operator_can_view_cha
PHPUnit 7.5.15 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 5.28 seconds, Memory: 26.00 MB
There was 1 failure:
1) Tests\Feature\ChangesTest::an_operator_can_view_changes
Failed asserting that false is true.
/var/www/tests/Feature/ChangesTest.php:26
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
without first fail, second success:
root@918e53969b70:/var/www# phpunit --filter an_operator_can_view_cha
PHPUnit 7.5.15 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 707 ms, Memory: 20.00 MB
There was 1 failure:
1) Tests\Feature\ChangesTest::an_operator_can_view_changes
Failed asserting that false is true.
/var/www/tests/Feature/ChangesTest.php:23
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
root@918e53969b70:/var/www# phpunit --filter an_operator_can_view_cha
PHPUnit 7.5.15 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 660 ms, Memory: 20.00 MB
OK (1 test, 1 assertion)