Ruby on rails 测试控制器问题(在Rails 3.2中)

Ruby on rails 测试控制器问题(在Rails 3.2中),ruby-on-rails,rspec,activesupport,Ruby On Rails,Rspec,Activesupport,我为管理记录创建了一个名为“record_search”的内部gem,其中一个应用程序中包含一个名为“ApiController”的控制器,我需要挂接授权行为以防止数据被公开访问,因此我添加了一个ActiveSupport::Concern模块,允许应用程序添加一个before_过滤器。如何在使用过滤器之前正确测试此功能?(使用rspec) 在创业板: app/controllers/api_controller.rb module RecordSearch class ApiControl

我为管理记录创建了一个名为“record_search”的内部gem,其中一个应用程序中包含一个名为“ApiController”的控制器,我需要挂接授权行为以防止数据被公开访问,因此我添加了一个ActiveSupport::Concern模块,允许应用程序添加一个before_过滤器。如何在使用过滤器之前正确测试此功能?(使用rspec)

在创业板:

app/controllers/api_controller.rb

module RecordSearch
  class ApiController < ApplicationController
    respond_to :json

    def search
      render json: #app-specific code
    end

    def find
      render json: #app-specific code
    end

    include Extensions #define any authorization logic here
  end
end
module RecordSearch::Extensions
  extend ActiveSupport::Concern
  include ApplicationHelper #defines current_user method

  included do
    before_filter :require_premium_user, :only => [:find,:search]
  end

  private

  def require_premium_user
    unless current_user
      return render :json => {"error" => "not authorized"}
    end
  end
end

假设
controller
是当前正在测试的控制器,那么您可以使用:

describe "searching when not a premimum user" do
   let(:user) { ... } # code to create a regular user
   before { ... } # code to login user
   it "should return an error message" do
      expect(controller).to receive(:render).with({"error" => "not authorized"})
      # code to trigger find or search
   end
end
为高级用户提供了类似的示例。另请参见,尽管我不熟悉Rails断言的使用

另外,实际的
require\u premium\u用户
代码对我来说没有意义