我在我的封闭系统Ruby on Rails应用程序中(使用Rails 4.1.5和Rspec 3.0),使用Pundit宝石进行授权。
我已经配置了我的应用策略,当用户未按照Pundit文档中的建议定义时,会引发异常:
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
raise Pundit::NotAuthorizedError, "must be logged in" unless user
@user = user
@record = record
end
end
我该如何编写一个规范以验证每个 Pundit 策略是否拒绝空用户?
我尝试了以下内容:
RSpec.describe PostPolicy do
it "should raise an exception when users aren't logged in" do
expect(AccountFolderPolicy.new(nil, record)).to raise_error(Pundit::NotAuthorizedError)
end
end
但是它报错了。
Failure/Error: expect(PostPolicy.new(nil, record)).to raise_error(Pundit::NotAuthorizedError)
Pundit::NotAuthorizedError:
must be logged in
有没有关于如何正确测试这个的建议?
expect { ... }.to raise_error
,而不是expect( ... ).to raise_error()
。 - Roman Kiselenko