【问题标题】:RSpec authorization testing with raise_error not working使用 raise_error 的 RSpec 授权测试不起作用
【发布时间】:2013-05-05 21:39:22
【问题描述】:

我正在尝试测试未登录用户的行为方式

  describe "not logged in user" do
   user_no_rights

    it "can't access action index" do
      expect(get :index).to raise_error(CanCan::AccessDenied)
    end
  end

我运行 rspec 时的输出

  Failure/Error: expect(get :index).to raise_error("CanCan::AccessDenied:You are not authorized to access this page.")
     CanCan::AccessDenied:
       You are not authorized to access this page.

所以看起来提出了正确的执行,但为什么规范没有通过?

【问题讨论】:

  • 在引发错误expect 的情况下可能需要传递一个块:expect{ get :index }.to raise_error(CanCan::AccessDenied)
  • 谢谢@ThomasKlemm!

标签: ruby-on-rails rspec cancan rspec-rails


【解决方案1】:

我已将规格更改为:

 describe "not logged in user" do
   user_no_rights

    it "can't access action index" do
      expect{get :index}.to raise_error(CanCan::AccessDenied)
    end
 end

它有效。向托马斯致敬! :-)

【讨论】:

    猜你喜欢
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多