diff --git a/spec/dsl_spec.rb b/spec/dsl_spec.rb index 97c936a..2119f1a 100644 --- a/spec/dsl_spec.rb +++ b/spec/dsl_spec.rb @@ -3,6 +3,8 @@ require "spec_helper" RSpec.describe "Pundit RSpec DSL" do + include Pundit::RSpec::PolicyExampleGroup + let(:fake_rspec) do double = class_double(RSpec::ExampleGroups) double.extend(::Pundit::RSpec::DSL) @@ -10,6 +12,11 @@ end let(:block) { proc { "block content" } } + let(:user) { double } + let(:other_user) { double } + let(:post) { Post.new(user) } + let(:policy) { PostPolicy } + it "calls describe with the correct metadata and without :focus" do expected_metadata = { permissions: %i[item1 item2], caller: instance_of(Array) } expect(fake_rspec).to receive(:describe).with("item1 and item2", match(expected_metadata)) do |&block| @@ -27,4 +34,38 @@ fake_rspec.permissions(:item1, :item2, :focus, &block) end + + describe "#permit" do + permissions :update? do + it "succeeds when action is permitted" do + expect(policy).to permit(user, post) + end + + context "when it fails" do + it "fails with a descriptive error message" do + expect do + expect(policy).to permit(other_user, post) + end.to raise_error(RSpec::Expectations::ExpectationNotMetError, <<~MSG.strip) + Expected PostPolicy to grant update? on Post but update? was not granted + MSG + end + end + + context "when negated" do + it "succeeds when action is not permitted" do + expect(policy).not_to permit(other_user, post) + end + + context "when it fails" do + it "fails with a descriptive error message" do + expect do + expect(policy).not_to permit(user, post) + end.to raise_error(RSpec::Expectations::ExpectationNotMetError, <<~MSG.strip) + Expected PostPolicy not to grant update? on Post but update? was granted + MSG + end + end + end + end + end end