ruby-on-rails - 带有 raise_error 的 RSpec 授权测试不起作用

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

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

  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.

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

最佳答案

我已将规范更改为:

 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

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

关于ruby-on-rails - 带有 raise_error 的 RSpec 授权测试不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16389401/

相关文章:

ruby-on-rails - Admin :Class --- CanCan 的未定义方法 `find'

ruby-on-rails - 如何在我的 rspec 测试中设置一个变量,以便 Controller 可以使用它进行查询?

ruby-on-rails - 请按语法排序 Mongoid Scope

javascript - Ruby on Rails 7 - 有没有办法实现 js.erb 文件?

ruby-on-rails - 在仍然调用原始实现的同时设置方法调用的期望

ruby-on-rails - 单页应用程序上的cancan

ruby-on-rails - Ruby on Rails 和 Prawn PDF

ruby rspec 不能与 simplecov 一起使用

ruby - rspec : it { is_expected. 中的这个符号是什么...}

ruby-on-rails-4 - 为什么 Pundit 不像 CanCanCan 那样与 Rolify 结合?