Get a fresh instance of your $user:
$this->assertTrue($user->fresh()->isMerchant());
Really though a GET route is not appropriate where you have side-effects.
Hope everyone is having a wonderful Monday afternoon!
I wrote a test to check to see if a user was being assigned a Role upon visiting a particular create() route. When I manually test this through the browser, I'm seeing new Users I create be assigned the proper Role, but my test keeps failing.
I was wondering if I could get some pointers on writing a test that can properly check for this? Test output below:
PHPUnit 6.5.5 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 123 ms, Memory: 14.00MB
There was 1 failure:
1) Tests\Feature\MerchantTest::a_user_can_become_a_merchant
Failed asserting that false is true.
/Users/mikevaldez/Dropbox/code/pintinerary-bootstrap/tests/Feature/MerchantTest.php:36
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Test code here:
/** @test */
public function a_user_can_become_a_merchant()
{
$user = factory(User::class)->create();
$this->actingAs($user)
->get(route('merchant.create'));
$this->assertTrue($user->isMerchant());
}
Here's the create function on my MerchantController:
public function create()
{
if (! auth()->user())
{
return redirect(route('register'));
}
if (! auth()->user()->isMerchant())
{
auth()->user()->becomeAMerchant();
}
return view('merchant.create');
}
Finally, here's the becomeAMerchant() and isMerchant() methods on my User model:
public function becomeAMerchant()
{
return $this->assignRole('merchant');
}
public function isMerchant()
{
return $this->hasRole('merchant');
}
Thanks so much for any advice! :)
Please or to participate in this conversation.