首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rspec before_filters失败

rspec before_filters失败
EN

Stack Overflow用户
提问于 2016-11-16 23:32:13
回答 1查看 140关注 0票数 0

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

代码语言:javascript
复制
> 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)>'

有人能帮我解决这件事吗谢谢。

这是我的规范:

代码语言:javascript
复制
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

这是控制器

代码语言:javascript
复制
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

这是spec/support/matchers/filter.rb中的matcher

代码语言:javascript
复制
RSpec::Matchers.define :filter do |kind, filter|
  match do |controller|
    extra = -> (x) {true}
    if filter[:except].present?
      extra = -> (x) { x.options[:unless].include?( "action_name == '#{filter[:except]}'") }
    elsif filter[:only].present?
      extra = -> (x) { x.options[:if].include?( "action_name == '#{filter[:only]}'") }
    end
    controller._process_action_callbacks.find{|x|x.kind == kind && x.filter == filter[:with] && extra.call(x)}
  end
end
EN

回答 1

Stack Overflow用户

发布于 2016-11-17 04:41:55

更好的测试是断言用户不能访问UploadsController (请求返回404),管理员可以访问(请求返回200)。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40644211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档