booni3
3 months ago
323
6
Testing

Inject user into form request - testing Autorization

Posted 3 months ago by booni3

I have feature tests hitting the post route to my form request and these all work fine.

However, I have some quite complicated authorization logic that I would like to test in isolation.

I am already doing something similar for some validation rules as follows:

public function setUp(): void
{
    parent::setUp();

    $this->subject = new \App\Http\Requests\OrderStoreRequest();

    $this->rules = $this->subject->rules();

    $this->validator = $this->app['validator'];
}

public function test_fields_are_valid()
{
    $this->assertTrue($this->validateField('country_code', 'GB'));
    $this->assertFalse($this->validateField('country_code', 'ZZ'));
}

I would now like to test the authorization method, but cannot work out how to inject a mock user into the form request. This does not work...

public function testAuthorize()
{
    $this
        ->actingAs(\factory(User::class)->create())
        ->assertTrue($this->subject->authorize());
}

Would much appricate any ideas on how this can be done with unit tests.

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