Forum: Rails I18n How to test several locales in functional tests

Posted by Mickaël (Guest)
on 2010-07-15 12:35
(Received via mailing list)
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 ;-)

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.
Posted by Christian Lescuyer (xtian)
on 2010-07-18 17:16
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
Posted by Henrik --- (malesca)
on 2010-07-18 21:21
(Received via mailing list)
On Jul 18, 5:16 pm, Christian Lescuyer <li...@ruby-forum.com> 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.
Posted by Mickaël (Guest)
on 2010-07-19 10:11
(Received via mailing list)
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.
Posted by Christian Lescuyer (xtian)
on 2010-07-19 10:49
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
Posted by Krzysztof Knapik (Guest)
on 2010-07-19 10:56
(Received via mailing list)
2010/7/18 Christian Lescuyer <lists@ruby-forum.com>:
>  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
Posted by Christian Lescuyer (xtian)
on 2010-07-19 11:01
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
Posted by Christian Lescuyer (xtian)
on 2010-07-19 11:07
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
Posted by Mickaël (Guest)
on 2010-07-19 14:52
(Received via mailing list)
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.