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:
parent
a0124c523b
commit
9d41dbc748
2 changed files with 16 additions and 5 deletions
|
@ -52,15 +52,15 @@ describe Pundit do
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
it "returns nil if the given policy scope can't be found" do
|
||||
|
@ -84,7 +84,7 @@ describe Pundit do
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
it "throws an exception if the given policy scope can't be found" do
|
||||
|
|
|
@ -87,10 +87,21 @@ module Customer
|
|||
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 Scope < Struct.new(:user, :scope)
|
||||
def resolve
|
||||
scope
|
||||
CommentScope.new(scope)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue