Skip to content

Instantly share code, notes, and snippets.

@davidjbeveridge
Forked from ahoward/mock_controller.rb
Created April 8, 2011 06:48
Show Gist options
  • Save davidjbeveridge/909415 to your computer and use it in GitHub Desktop.
Save davidjbeveridge/909415 to your computer and use it in GitHub Desktop.
# Don't know about better, but fewer lines:
def mock_controller
['test_request','test_response'].each {|fname| require "action_dispatch/testing/#{fname}" }
controller = ApplicationController.new
controller.perform_caching = true
controller.cache_store = ActiveSupport::Cache::MemoryStore.new
['request','response'].each {|r| controller.send("#{r}=", ActionDispatch.const_get("Test#{r.capitalize}").new) }
controller.initialize_template_class controller.response
controller.assign_shortcuts controller.request, controller.response
controller.default_url_options.merge!(DefaultUrlOptions) if defined?(DefaultUrlOptions)
controller
end
@davidjbeveridge
Copy link
Author

Agreed. It would be really nice if ApplicationController.new simply did that; but I suppose there are probably good reasons it doesn't. If nothing else, not attaching the Request and Response keeps us from being enslaved to the Request/Reply pattern. I suppose you could rewrite Rails just a bit to use Command/Event and support a completely different kind of application... don't know why you would though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment