title: Laravel: Testing Policies with Spatie Permissions Package
body: <p>I'm looking to find a way to test Policy methods in isolation. The policies work as intended when Feature testing (e.g. via http) but I am having a hard time trying to write tests for these when I just want to test the logic of the policy itself.</p>
  <p>My code:</p>
  <pre><code>class ItemPolicy
  {
      ...
  
      public function delete(User $user, Item $item)
      {
          return $this-&gt;can('delete_item') &amp;&amp; !$item-&gt;isDeletable();
      }
  
      ...
  }
  </code></pre>
  <pre><code>class ItemPolicyTest extends TestCase
  {
      use RefreshDatabase;
  
      private $user;
  
      public function setUp() : void
      {
          parent::setUp();
          
          $this-&gt;user = User::factory()-&gt;create();
          
          Permission::create(['name' =&gt; 'delete_item', 'guard_name' =&gt; 'web']);
          $role = Role::create(['name' =&gt; 'admin', 'guard_name' =&gt; 'web']);
          $role-&gt;givePermissionTo('delete_item');
          
          $this-&gt;user-&gt;assignRole(['admin']);
          
          $this-&gt;user = $this-&gt;user-&gt;fresh(); //just to make sure
          $this-&gt;app-&gt;make(\Spatie\Permission\PermissionRegistrar::class)-&gt;registerPermissions();
      }
  
      /** @test */
      public function it_will_not_allow_deletion()
      {
          $item = Item::factory()-&gt;cannotBeDeleted()-&gt;create();
  
          $this-&gt;be($this-&gt;user, 'web');
  
          $this-&gt;assertFalse(auth()-&gt;user()-&gt;can('delete', $item));
      }
  }
  </code></pre>
  <p>Setting a breakpoint in my Policy doesn't get triggered so I assume that Spatie has taken this over somewhere. So ultimately, I'm looking for a way to recreate this so I can test these in isolation. Any help appreciated!</p>