在Rails 4控制器中使用时,处理问题测试的最佳方法是什么?假设我有一个小问题.
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.