没有rspec before_filters

时间:2016-11-16 23:32:13

标签: ruby-on-rails ruby rspec

我是Rails的新手,我正在使用Boti's evolved way to test my project,但是我收到了以下错误:

LI

有人可以帮我解决这个问题,谢谢。

这是我的规范:

> Failures:

  1) ApplicationController UploadsController admin_only should filter :before and {:with=>:admin_only, :except=>:show}
     Failure/Error: extra = -> (x) { x.options[:unless].include?( "action_name == '#{filter[:except]}'") }

     NoMethodError:
       undefined method `options' for #<ActiveSupport::Callbacks::Callback:0x000006060f6598>
     # ./spec/support/matchers/filter.rb:5:in `block (3 levels) in <top (required)>'
     # ./spec/support/matchers/filter.rb:9:in `call'
     # ./spec/support/matchers/filter.rb:9:in `block (3 levels) in <top (required)>'
     # ./spec/support/matchers/filter.rb:9:in `find'
     # ./spec/support/matchers/filter.rb:9:in `block (2 levels) in <top (required)>'
     # ./spec/controllers/application_controller_spec.rb:37:in `block (4 levels) in <top (required)>'

这是控制器

RSpec.describe ApplicationController, type: :controller do
  include Devise::Test::ControllerHelpers
       describe UploadsController do
        context 'authentication' do
         specify{ expect(UploadsController).to filter(:before, with: :authenticate_user!)}
        end
        context 'admin_only' do
         specify{ expect(UploadsController).to filter(:before, with: :admin_only, except: :index)}
        end
      end
   end

这是spec / support / matchers / filter.rb中的匹配器

class UploadsController < ApplicationController
   before_action :authenticate_user!
   before_action :admin_only, :except => :index
   def index
    begin
      @uploads = Upload.all
    rescue
      redirect_to uploads_alert_uploads_path(:error_path => request.url, :error_time => DateTime.now)
    end
  end
end

1 个答案:

答案 0 :(得分:1)

更好的测试是断言用户无法访问UploadsController(请求返回404)并且管理员可以(请求返回200)。

断言调用过滤器会将测试与admin_only方法的实现联系起来,而不是限制用户可以访问的最终结果。