1
0
Fork 0
mirror of https://github.com/varvet/pundit.git synced 2022-11-09 12:30:11 -05:00

More exact checks in specs with Comment scopes

The Comment scope in the specs now returns an object wrapping what was
originally passed in. This makes more sense than passing the object all
the way through, and lets the spec check that the correct method was run.
This commit is contained in:
Ian Young 2017-10-11 17:18:39 -05:00
parent a0124c523b
commit 9d41dbc748
2 changed files with 16 additions and 5 deletions

View file

@ -52,15 +52,15 @@ describe Pundit do
end end
it "returns an instantiated policy scope given an active model class" do it "returns an instantiated policy scope given an active model class" do
expect(Pundit.policy_scope(user, Comment)).to eq Comment expect(Pundit.policy_scope(user, Comment)).to eq CommentScope.new(Comment)
end end
it "returns an instantiated policy scope given an active record relation" do it "returns an instantiated policy scope given an active record relation" do
expect(Pundit.policy_scope(user, comments_relation)).to eq comments_relation expect(Pundit.policy_scope(user, comments_relation)).to eq CommentScope.new(comments_relation)
end end
it "returns an instantiated policy scope given an empty active record relation" do it "returns an instantiated policy scope given an empty active record relation" do
expect(Pundit.policy_scope(user, empty_comments_relation)).to eq empty_comments_relation expect(Pundit.policy_scope(user, empty_comments_relation)).to eq CommentScope.new(empty_comments_relation)
end end
it "returns nil if the given policy scope can't be found" do it "returns nil if the given policy scope can't be found" do
@ -84,7 +84,7 @@ describe Pundit do
end end
it "returns an instantiated policy scope given an active model class" do it "returns an instantiated policy scope given an active model class" do
expect(Pundit.policy_scope!(user, Comment)).to eq Comment expect(Pundit.policy_scope!(user, Comment)).to eq CommentScope.new(Comment)
end end
it "throws an exception if the given policy scope can't be found" do it "throws an exception if the given policy scope can't be found" do

View file

@ -87,10 +87,21 @@ module Customer
end end
end end
class CommentScope
attr_reader :original_object
def initialize(original_object)
@original_object = original_object
end
def ==(other)
original_object == other.original_object
end
end
class CommentPolicy < Struct.new(:user, :comment) class CommentPolicy < Struct.new(:user, :comment)
class Scope < Struct.new(:user, :scope) class Scope < Struct.new(:user, :scope)
def resolve def resolve
scope CommentScope.new(scope)
end end
end end
end end