【发布时间】:2014-02-26 23:34:12
【问题描述】:
在 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.
【问题讨论】:
标签: ruby-on-rails rspec controller activesupport-concern