xtremer360
4 months ago
1350
34
Testing

Mocking authorization can method

Posted 4 months ago by xtremer360

I'm trying to unit test a form request class authorize method which I've included below. I'm wanting to do it this way so that I can check the policy update method separately and that way I know that what comes out of there is based on the user role and such. Does anyone see anything wrong with doing it in this manner? Also am I mocking the correct class here?

public function authorized_returns_true_when_authenticated_user_can_update_venue()
{
    $user = UserFactory::new()->make();

    $loggedInUser = $this->actingAs($user);

    Mockery::mock($loggedInUser)->shouldReceive('can')->withArgs(['update', $user])->andReturn(true);

    $subject = new UpdateRequest();

    $this->assertFalse($subject->authorize());
}
public function authorize()
{
    $venue = $this->route('venue');

    if (! Auth::check()) {
        return false;
    }

    return $this->user()->can('update', $venue);
}

Please sign in or create an account to participate in this conversation.