我正在尝试将以下规范转换为新的Expect语法,有人可以帮忙吗?
describe PostPolicy do
subject { PostPolicy }
permissions :create? do
it "denies access if post is published" do
should_not permit(User.new(:admin => false), Post.new(:published => true))
end
it "grants access if post is published and user is an admin" do
should permit(User.new(:admin => true), Post.new(:published => true))
end
it "grants access if post is unpublished" do
should permit(User.new(:admin => false), Post.new(:published => false))
end
end
end
我试过了,但是permit()
没用,因为返回了匹配器- RSpec::Matchers::DSL::Matcher
:
specify { expect(permit(@user, @post)).to be_true }
您必须subject
显式调用,因为隐式接收器仅适用于should
。这里和这里有更多信息。
在您的示例中,这应该起作用:
describe PostPolicy do
subject { PostPolicy }
permissions :create? do
it "denies access if post is published" do
expect(subject).not_to permit(User.new(:admin => false), Post.new(:published => true))
end
it "grants access if post is published and user is an admin" do
expect(subject).not_to permit(User.new(:admin => true), Post.new(:published => true))
end
it "grants access if post is unpublished" do
expect(subject).not_to permit(User.new(:admin => false), Post.new(:published => false))
end
end
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句