Skip to content

Commit

Permalink
Merge pull request #6 from cawel/controller_helper
Browse files Browse the repository at this point in the history
Functional tests support
  • Loading branch information
francesc committed Jan 19, 2012
2 parents 5a3fc67 + df75632 commit aba6830
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.rdoc
Expand Up @@ -174,6 +174,16 @@ In most cases you won't want to have the backend in several languages, you can s
end
end

== Testing

Once your app is locale-aware, the routes are dependent on the locale. This means that in functional tests, you need to explicitly include the locale like so:

get :show, :id => 1, :locale => 'en'

But in case you would prefer the locale to be implicit or simply because you don't want to add the locale param to all your previous functional tests, you can require the 'lib/controller_test_helper' file (typically from your test helper file) and then this will work fine:

get :show, :id => 1

== TODO

Help is welcome ;)
Expand Down Expand Up @@ -213,4 +223,4 @@ There are other two projects for translating routes in Rails (which I know of),

== Other i18n related projects

If you also need to translate models check out: https://github.com/francesc/rails-translate-models
If you also need to translate models check out: https://github.com/francesc/rails-translate-models
19 changes: 19 additions & 0 deletions lib/controller_test_helper.rb
@@ -0,0 +1,19 @@
# This monkey patch injects the locale in the controller's params
# Reason: ActionController::TestCase doesn't support "default_url_options"
#
# Example: when the request you are testing needs the locale, e.g.
# get :show, :id => 1, :locale => 'en'
#
# if the monkey patch was loaded, you can just do:
# get :show, :id => 1

class ActionController::TestCase

module Behavior
def process_with_default_locale(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
parameters = { :locale => I18n.default_locale }.merge(parameters || {} )
process_without_default_locale(action, parameters, session, flash, http_method)
end
alias_method_chain :process, :default_locale
end
end

0 comments on commit aba6830

Please sign in to comment.