在 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.
有帮助吗?

解决方案

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

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

方法一:无需路由或响应测试

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

describe MyControllerConcern do

  before do
    class FakesController < ApplicationController
      include MyControllerConcern
    end
  end
  after { Object.send :remove_const, :FakesController }
  let(:object) { FakesController.new }

  describe 'my_method_to_test' do
    it { 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 { routes.draw {
    get 'fake_action' => 'anonymous#fake_action'
  } }


  describe 'my_method_to_test' do
    before { get :fake_action }
    it { expect(response).to redirect_to('/an_url') }
  end
end

你可以看到我们必须将匿名控制器包装在一个 controller(ApplicationController). 。如果您的类是从另一个类继承的 ApplicationController, ,您需要对此进行调整。

此外,为了使其正常工作,您必须在您的 规范助手.rb 文件:

config.infer_base_class_for_anonymous_controllers = true

笔记:继续测试您关心的问题是否包含在内

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

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

其他提示

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

我更喜欢 anonymous controller rspec 支持 http://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/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 您的应用程序中定义的任何其他基本控制器都没有。这样您就可以独立于任何控制器来测试关注点。如果您希望在基本控制器中定义某些方法,只需对它们进行存根即可。

此外,最好避免重新输入关注模块名称,因为它有助于避免复制粘贴错误。很遗憾, described_class 无法在传递给的块中访问 controller(ActionController::Base), ,所以我用 #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归因
不隶属于 StackOverflow
scroll top