Cómo probar una preocupación del controlador en Rails 4
¿Cuál es la mejor manera de manejar las pruebas de preocupaciones cuando se usa en los controladores Rails 4? Digamos que tengo una preocupación trivialCitations
.
module Citations
extend ActiveSupport::Concern
def citations ; end
end
El comportamiento esperado bajo prueba es que cualquier controlador que incluya esta preocupación obtendría estocitations
punto final.
class ConversationController < ActionController::Base
include Citations
end
Sencillo.
ConversationController.new.respond_to? :yelling #=> true
Pero, ¿cuál es la forma correcta de probar esta preocupación de forma aislada?
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
Lamentablemente, esto falla.
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)>'
Este es un ejemplo artificial. En mi aplicación, recibo un error diferente.
RuntimeError:
@routes is nil: make sure you set it in your test's setup method.