Hello everyone,
Today it is like I was punched in the face.
I have all test running and it were all green, all passing.
Today I decided to run all the test to see if everything it is ok. But when I run the tests sometimes they pass, and sometime they fail.
Any Thoughts?
MacBook-Pro-de-Vitor:w7code miguel$ php artisan test --filter a_superadmin_can_see_any_profile_except_other_superadmin
PASS Tests\Feature\Models\User\ProfileTest
✓ a superadmin can see any profile except other superadmin
Tests: 1 passed
Time: 0.49s
MacBook-Pro-de-Vitor:w7code miguel$ php artisan test --filter a_superadmin_can_see_any_profile_except_other_superadmin
FAIL Tests\Feature\Models\User\ProfileTest
✕ a superadmin can see any profile except other superadmin
Tests: 1 failed
Expected status code 404 but received 302. Failed asserting that 404 is identical to 302.
at tests/Feature/Models/User/ProfileTest.php:43
39|
40| // a user cannot see profile using /settings/profile/username
41| // only using /settings/profile
42| $this->get(route('settings.profile.index', $user->username))
> 43| ->assertStatus(404);
44|
45| $this->get(route('settings.profile.index'))
46| ->assertStatus(200);
47|
MacBook-Pro-de-Vitor:w7code miguel$ php artisan test --filter a_superadmin_can_see_any_profile_except_other_superadmin
PASS Tests\Feature\Models\User\ProfileTest
✓ a superadmin can see any profile except other superadmin
Tests: 1 passed
Time: 0.49s
<?php
namespace Tests\Feature\Models\User;
use Tests\TestCase;
use App\Models\User;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Session;
class ProfileTest extends TestCase
{
use RefreshDatabase, DatabaseMigrations;
public function setUp(): void
{
parent::setUp();
// you can call
$this->artisan('db:seed');
// or
// $this->seed();
}
/**
* Profile See Tests
*/
/** @test */
public function a_superadmin_can_see_any_profile_except_other_superadmin()
{
$user = factory('App\Models\User')->create();
$user->assignRole('superadmin');
$this->signIn($user);
// a user cannot see profile using /settings/profile/username
// only using /settings/profile
$this->get(route('settings.profile.index', $user->username))
->assertStatus(404);
$this->get(route('settings.profile.index'))
->assertStatus(200);
$superadmin = factory(User::class)->create();
$superadmin->assignRole('superadmin');
$this->get(route('settings.profile.index', $superadmin->username))
->assertStatus(404);
$admin = factory(User::class)->create();
$admin->assignRole('admin');
$this->get(route('settings.profile.index', $admin->username))
->assertStatus(200);
$moderator = factory(User::class)->create();
$moderator->assignRole('moderator');
$this->get(route('settings.profile.index', $moderator->username))
->assertStatus(200);
$premium = factory(User::class)->create();
$premium->assignRole('premium');
$this->get(route('settings.profile.index', $premium->username))
->assertStatus(200);
$otherUser = factory(User::class)->create();
$this->get(route('settings.profile.index', $otherUser->username))
->assertStatus(200);
}
This I my TesCase.php:
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected function signIn($user = null, $password = null)
{
$user = $user ?: factory('App\Models\User')->create();
$user->assignRole('user');
$this->actingAs($user);
return $this;
}
}
Good find @miguellima, I was scratching my head about the likely cause.
Maybe I could suggest that your default factory adopt the happy path (if that means isPublic is true, then so be it). Then use a factory state to set the different value for those special cases: