nichtmonti
nichtmonti

Reputation: 43

Laravel Mock Policy during Testing

I would like to verify, that a Policy method is called at the correct time with the correct parameters.

In my controller I call $this->authorize('edit', $entry) and would like to ensure the edit method in my entryPolicy.class is called.

I have tried Mocking the Gate Facade, which fails because I am not logged in during testing. (Unit test, no Feature test)

I have tried Mocking the Policy using Mockery, but since the Mock is ignored since the Policy is already instantiated and registered when the test runs. (or so i presume)

Last I tried registering the mocked Policy using the Gate Facade, which didn't work either

Ideally, the working code should look something like this:

 $uut=\Mockery::mock(entryPolicy::class);
 $uut->shouldReceive('update')->andReturn();
 $controler->update($request, $entry);

Any help is greatly appreciated

Upvotes: 4

Views: 821

Answers (1)

luke bouch
luke bouch

Reputation: 49

Can I ask why you are testing this with a unit test as opposed to a feature test? Since it is a controller you are testing, are you actually hitting the controller with an HTTP request in your test?

When I was testing a controller, I wanted to mock out a policy and the following worked for me.

$this->partialMock(ClaimPolicy::class, function(MockInterface $mock) {
    $mock->shouldReceive('updateClaimNumber')->andReturnFalse();
});

Upvotes: 0

Related Questions