如何在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

方法2:测试响应

当您的关注点包含路由或您需要测试响应,渲染等...时,您需要使用匿名控制器运行测试。这使您可以访问所有与控制器相关的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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Rails 4中测试控制器问题

来自分类Dev

Rails 4测试控制器故障

来自分类Dev

Rails 4-控制器测试错误,活动记录关联问题

来自分类Dev

如何在 Rails 测试中显示控制器错误消息?

来自分类Dev

Rails/Rspec - 如何在控制器测试中存根模型方法?

来自分类Dev

如何告诉Rails卸载控制器(在Rspec测试中)

来自分类Dev

使用Rspec进行控制器测试中的Rails关联问题

来自分类Dev

使用Rails 6上的Rspec测试控制器问题

来自分类Dev

如何在MVC中测试控制器类?

来自分类Dev

如何在控制器中测试角度元素绑定

来自分类Dev

如何在控制器中测试角度$ destroy

来自分类Dev

如何在Ember控制器中测试.observes()?

来自分类Dev

如何在角度控制器中测试本地功能?

来自分类Dev

如何在指令中测试控制器?

来自分类Dev

如何在控制器中测试实例变量?

来自分类Dev

如何在AngularJS控制器中测试服务?

来自分类Dev

如何在控制器中测试角度$ destroy

来自分类Dev

如何在控制器中测试角度元素绑定

来自分类Dev

如何在MVC中测试控制器类?

来自分类Dev

从Rails 4中的控制器注销

来自分类Dev

从Rails 4中的控制器注销

来自分类Dev

如何在Ruby on Rails中传递控制器参数

来自分类Dev

如何在rails的控制器中包含用户信息

来自分类Dev

如何在Rails 4的控制器内部生成的变量中覆盖create动作

来自分类常见问题

如何在Rails 4中为控制器或操作覆盖X-Frame-Options

来自分类Dev

如何在Rails 4的控制器中的方法之间共享变量?

来自分类Dev

如何在Rails 4中组织控制器目录而不弄乱路由

来自分类Dev

如何在Rails 4的控制器内部生成的变量中覆盖create动作

来自分类Dev

如何在Rails 4中使用按钮调用控制器方法

Related 相关文章

热门标签

归档