Skip to content

Commit

Permalink
Make sure we test the rspec DSL explicitly
Browse files Browse the repository at this point in the history
We're testing it by proxy since it's being used in some tests, but these tests are _specific_ to the matchers themselves.
  • Loading branch information
Burgestrand committed Oct 11, 2024
1 parent e3579f6 commit fb00037
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions spec/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
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)
double
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|
Expand All @@ -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

0 comments on commit fb00037

Please sign in to comment.