Ruby on rails 使用expect语法对权威进行RSpec测试
我正在尝试将以下规范转换为新的expect语法,有人能帮忙吗Ruby on rails 使用expect语法对权威进行RSpec测试,ruby-on-rails,rspec,pundit,Ruby On Rails,Rspec,Pundit,我正在尝试将以下规范转换为新的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
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 }
您必须显式调用
主题
,因为隐式接收器仅适用于应该
。更多信息和信息
在您的示例中,这应该有效:
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
另一种选择是使用隐式主题语法
describe PostPolicy do
subject { PostPolicy }
permission :create? do
it { is_expected.not_to permit(User.new(admin: false), Post.new(published: true)) }
end
end
is\u expected
只需调用expect(subject)
。这使得单行程序更加方便。谢谢,你几乎是对的,但是指定主题是必需的,否则它会失败,因为ArgumentError:参数数目错误(0代表2)
。这为什么会被否决?我不知道。如果您觉得有用,请向上投票:)我在测试策略和范围测试时也会遇到同样的问题