繁体   English   中英

如何在 Rails 4 中测试控制器关注点

[英]How to test a Controller Concern in Rails 4

在 Rails 4 控制器中使用时处理关注点测试的最佳方法是什么? 说我有一个小问题Citations

module Citations
    extend ActiveSupport::Concern
    def citations ; end
end

测试中的预期行为是任何包含此问题的控制器都将获得此citations端点。

class ConversationController < ActionController::Base
    include Citations
end

简单的。

ConversationController.new.respond_to? :yelling #=> true

但是,孤立地测试这种担忧的正确方法是什么?

class CitationConcernController < ActionController::Base
    include Citations
end

describe CitationConcernController, type: :controller do
    it 'should add the citations endpoint' do
        get :citations
        expect(response).to be_successful
    end
end

不幸的是,这失败了。

CitationConcernController
  should add the citations endpoint (FAILED - 1)

Failures:

  1) CitationConcernController should add the citations endpoint
     Failure/Error: get :citations
     ActionController::UrlGenerationError:
       No route matches {:controller=>"citation_concern", :action=>"citations"}
     # ./controller_concern_spec.rb:14:in `block (2 levels) in <top (required)>'

这是一个人为的例子。 在我的应用程序中,我收到了不同的错误。

RuntimeError:
  @routes is nil: make sure you set it in your test's setup method.

您会发现许多建议告诉您使用共享示例并在您包含的控制器范围内运行它们。

我个人觉得这太过分了,更喜欢单独执行单元测试,然后使用集成测试来确认我的控制器的行为。

方法 1:不进行路由或响应测试

创建一个假控制器并测试它的方法:

describe MyControllerConcern do
  before do
    class FakesController < ApplicationController
      include MyControllerConcern
    end
  end

  after do
    Object.send :remove_const, :FakesController 
  end

  let(:object) { FakesController.new }

  it 'my_method_to_test' do
    expect(object).to eq('expected result')
  end

end

方法二:测试响应

当您关注的问题包含路由或您需要测试响应、渲染等时……您需要使用匿名控制器运行您的测试。 这允许您访问所有与控制器相关的 rspec 方法和帮助程序:

describe MyControllerConcern, type: :controller do
  controller(ApplicationController) do
    include MyControllerConcern

    def fake_action; redirect_to '/an_url'; end
  end

  before do
    routes.draw {
      get 'fake_action' => 'anonymous#fake_action'
    }
  end
    
  describe 'my_method_to_test' do
    before do
      get :fake_action 
    end

    it do
      expect(response).to redirect_to('/an_url') 
    end
  end
end

如您所见,我们使用controller(ApplicationController)定义了匿名控制器。 如果您的测试涉及除ApplicationController之外的另一个类,您将需要对此进行调整。

同样为了使其正常工作,您必须在spec_helper.rb文件中配置以下内容:

config.infer_base_class_for_anonymous_controllers = true

注意:继续测试您的担忧是否包括在内

测试您的关注类是否包含在您的目标类中也很重要,一行就足够了:

describe SomeTargetedController do
  it 'includes MyControllerConcern' do
    expect(SomeTargetedController.ancestors.include? MyControllerConcern).to be(true) 
  end
end

从投票最多的答案中简化方法 2。

我更喜欢 rspec http://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller 中支持的anonymous controller

你会去做的:

describe ApplicationController, type: :controller do
  controller do
    include MyControllerConcern

    def index; end
  end

  describe 'GET index' do
    it 'will work' do
      get :index
    end
  end
end

请注意,您需要描述ApplicationController并设置类型,以防默认情况下不会发生这种情况。

我的答案可能看起来比@Benj 和 @Calin 的答案复杂一些,但它有其优点。

describe Concerns::MyConcern, type: :controller do

  described_class.tap do |mod|
    controller(ActionController::Base) { include mod }
  end

  # your tests go here
end

首先,我建议使用匿名控制器,它是ActionController::Base的子类,而不是ApplicationController定义的任何其他基本控制器。 通过这种方式,您可以独立于任何控制器来测试关注点。 如果您希望在基本控制器中定义某些方法,只需存根它们即可。

此外,避免重新输入关注模块名称是一个好主意,因为它有助于避免复制粘贴错误。 不幸的是,在传递给controller(ActionController::Base)的块中无法访问described_class ,因此我使用#tap方法创建另一个绑定,将described_class存储在局部变量中。 这在使用版本化 API 时尤其重要。 在这种情况下,在创建新版本时复制大量控制器是很常见的,然后很容易犯这种微妙的复制粘贴错误。

我正在使用一种更简单的方法来测试我的控制器问题,不确定这是否是正确的方法,但似乎比上面更简单并且对我有意义,它使用包含的控制器的范围。 如果此方法有任何问题,请告诉我。 样品控制器:

class MyController < BaseController
  include MyConcern

  def index
    ...

    type = column_type(column_name)
    ...
  end

结尾

我的控制器关注:

module MyConcern
  ...
  def column_type(name)
    return :phone if (column =~ /phone/).present?
    return :id if column == 'id' || (column =~ /_id/).present?
   :default
  end
  ...

end

关注的规格测试:

require 'spec_helper'

describe SearchFilter do
  let(:ac)    { MyController.new }
  context '#column_type' do
    it 'should return :phone for phone type column' do
      expect(ac.column_type('phone')).to eq(:phone)
    end

    it 'should return :id for id column' do
      expect(ac.column_type('company_id')).to eq(:id)
    end

    it 'should return :id for id column' do
      expect(ac.column_type('id')).to eq(:id)
    end

    it 'should return :default for other types of columns' do
      expect(ac.column_type('company_name')).to eq(:default)
    end
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM