Skip to content

Instantly share code, notes, and snippets.

@lcguida
Created October 15, 2015 09:52
Show Gist options
  • Save lcguida/4282a46b7a05bb1e5fc3 to your computer and use it in GitHub Desktop.
Save lcguida/4282a46b7a05bb1e5fc3 to your computer and use it in GitHub Desktop.
Creates a `grant_permission_to` matcher to test Pundit policies.
# spec/support/pundit.rb
# Creates a 'grant_permission_to' matcher to better test Pundit policies
# See: http://thunderboltlabs.com/blog/2013/03/27/testing-pundit-policies-with-rspec/
RSpec::Matchers.define :grant_permission_to do |action|
match do |policy|
policy.public_send("#{action}?")
end
description do
"grant permition to #{action}"
end
failure_message do |policy|
"#{policy.class} does not permit #{action} on #{policy.record} for #{policy.user.inspect}."
end
failure_message_when_negated do |policy|
"#{policy.class} does not forbid #{action} on #{policy.record} for #{policy.user.inspect}."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment