How to test several locales in functional tests

Hi all,

Yesterday I have overcome a mountain thank’s to Iain (
Exception is not raise with #t method on Rails 2.3.5
http://tinyurl.com/3yeoh34),
so, it’s cool :wink:

But now, I want to test every locales (for the moment ‘en’ and ‘fr’)
in my functionals tests.

I have trying to implement a loop which set I18n.locale variable in
setup context but it seem not working.
(Like my Gist http://gist.github.com/476774)

If you have already do this, your help is grandly appreciate.

Best regards,

Mickaël.

Same here. This works:

I18n.locale = :test
assert_equal :test, I18n.locale
assert_equal ‘,’, I18n.translate(‘number.format.separator’)

But this doesn’t:

@product = Factory.create(:product, :price => 49.95)
I18n.locale = :test
get :edit, :id => @product.permalink
assert_select “#product_price[value=‘49,95’]”

We’re testing the view (@response.body), here, not the controller. But I
can’t find where the view is rendered, and why I18n.locale is not used.

Best regards,
Xtian

On Jul 18, 5:16 pm, Christian L. [email protected] wrote:

get :edit, :id => @product.permalink
assert_select “#product_price[value=‘49,95’]”

We’re testing the view (@response.body), here, not the controller. But I
can’t find where the view is rendered, and why I18n.locale is not used.

As a sanity check… If these are controller tests, did you call
integrate_views? Otherwise they do not actually render the view.

Hi all,

I have two locales FR and EN.

So, to test EN locale, I make volontary mistakes in my
alert_collaborator EN YML File translation to test if the EN locale is
set.

First of all, in my /config/environnement.rb I have
Rails::Initializer.run do |config|
[…]
config.i18n.load_path += Dir[Rails.root.join(‘config’, ‘locales’,
‘**’, ‘*.{rb,yml}’)]
config.i18n.default_locale = :fr
end

If i’m starting my prospects_controller_test.rb with no mistakes in my
FR YML File, there are no problem.
Started

Finished in 6.043841 seconds.
33 tests, 50 assertions, 0 failures, 0 errors

So, if a specify “I18n.locale = :en” in my setup functional test, like
this.

setup do
  UserSession.create(Factory(:user))
  Factory(:agency)
  Factory(:feedback)
  @alert_collaborator = Factory.stub(:alert_collaborator)
  @alert_collaborator.id = 1001

  AlertCollaborator.stubs(:find).returns(@alert_collaborator)
  AlertCollaborator.stubs(:find).with(:all,

anything).returns([@alert_collaborator])

  I18n.locale = :en

end

There are no problem even if EN YML File translation contain an error.
It’s strange.

But, if I remplace “I18n.locale = :en” by “I18n.default_locale = :en”,
like this.

setup do
  UserSession.create(Factory(:user))
  Factory(:agency)
  Factory(:feedback)
  @alert_collaborator = Factory.stub(:alert_collaborator)
  @alert_collaborator.id = 1001

  AlertCollaborator.stubs(:find).returns(@alert_collaborator)
  AlertCollaborator.stubs(:find).with(:all,

anything).returns([@alert_collaborator])

  I18n.default_locale = :en

end

Mistakes appears.

Loaded suite functional/hr/alert_collaborators_controller_test
Started
…EEEE…EEE…
Finished in 5.872202 seconds.

So my conclusion is :

Functional test take default_locale variable while is running his
tests.
If we specify a locale changement in functional test with #locale
method from I18n module, it mustn’t be take care of it.

Thanks Henrik,

As a sanity check… If these are controller tests, did you call
integrate_views? Otherwise they do not actually render the view.

It seems integrate_views is only available for RSpec. I’m using Rails
functional test.

Best regards
Xtian

2010/7/18 Christian L. [email protected]:

get :edit, :id => @product.permalink
assert_select “#product_price[value=‘49,95’]”

We’re testing the view (@response.body), here, not the controller. But I
can’t find where the view is rendered, and why I18n.locale is not used.

Locale should be set with each single request, so you should have sth
like:
get :edit, :id => @product.permalink, :locale => :en
assert_response :success # I would ensure response 200 status here
assert_select “#product_price[value=‘49,95’]”


Regards
KK

Thank you Krzysztof,

Locale should be set with each single request, so you should have sth
like:
get :edit, :id => @product.permalink, :locale => :en

Actually, I did try this, and it doesn’t work. I suspect it works if the
:locale parameter is integrated in the route.

Best regards
Xtian

Merci Mickaël,

So my conclusion is :

Functional test take default_locale variable while is running his
tests.

It worked for me! I’m not sure it’s a general solution, as I use Spree
and I also had to override the Spree config. Here goes:

context “on GET to :edit with :test locale” do
setup do
@product = Factory.create(:product, :price => 49.95)
I18n.default_locale = :test
Spree::Config.set(:default_locale => :test)
get :edit, :id => @product.permalink
end
should “format the price properly” do
assert_select “#product_price[value=‘49,95’]”
end
end

My test.yml file:

test:
number:
format:
separator: “,”

Xtian

You’re welcome Christian.

To check every functional tests with available locales. We can do like
my Gist http://gist.github.com/481319

Best regards,

Mickaël.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs