# How to test several locales in functional tests

**URL:** <https://www.ruby-forum.com/t/how-to-test-several-locales-in-functional-tests/191620>\
**Category:** Rails I18n\
**Created:** [July 15, 2010, 12:35pm UTC](https://www.ruby-forum.com/t/how-to-test-several-locales-in-functional-tests/191620 "2010-07-15T12:35:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MickaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MickaSSSSl](https://www.ruby-forum.com/u/MickaSSSSl)\
**Post date:** [July 15, 2010, 12:35pm UTC](https://www.ruby-forum.com/t/how-to-test-several-locales-in-functional-tests/191620/1 "2010-07-15T12:35:01Z")

</div>

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](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 [OUTPUT · GitHub](http://gist.github.com/476774))

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

Best regards,

Mickaël.

---

<div class="post-metadata">

**Author:** ![MickaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MickaSSSSl](https://www.ruby-forum.com/u/MickaSSSSl)\
**Post date:** [July 18, 2010, 5:16pm UTC](https://www.ruby-forum.com/t/how-to-test-several-locales-in-functional-tests/191620/2 "2010-07-18T17:16:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MickaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MickaSSSSl](https://www.ruby-forum.com/u/MickaSSSSl)\
**Post date:** [July 18, 2010, 9:21pm UTC](https://www.ruby-forum.com/t/how-to-test-several-locales-in-functional-tests/191620/3 "2010-07-18T21:21:17Z")

</div>

On Jul 18, 5:16 pm, Christian L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![MickaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MickaSSSSl](https://www.ruby-forum.com/u/MickaSSSSl)\
**Post date:** [July 19, 2010, 10:11am UTC](https://www.ruby-forum.com/t/how-to-test-several-locales-in-functional-tests/191620/4 "2010-07-19T10:11:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MickaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MickaSSSSl](https://www.ruby-forum.com/u/MickaSSSSl)\
**Post date:** [July 19, 2010, 10:49am UTC](https://www.ruby-forum.com/t/how-to-test-several-locales-in-functional-tests/191620/5 "2010-07-19T10:49:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MickaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MickaSSSSl](https://www.ruby-forum.com/u/MickaSSSSl)\
**Post date:** [July 19, 2010, 10:56am UTC](https://www.ruby-forum.com/t/how-to-test-several-locales-in-functional-tests/191620/6 "2010-07-19T10:56:38Z")

</div>

2010/7/18 Christian L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![MickaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MickaSSSSl](https://www.ruby-forum.com/u/MickaSSSSl)\
**Post date:** [July 19, 2010, 11:01am UTC](https://www.ruby-forum.com/t/how-to-test-several-locales-in-functional-tests/191620/7 "2010-07-19T11:01:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MickaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MickaSSSSl](https://www.ruby-forum.com/u/MickaSSSSl)\
**Post date:** [July 19, 2010, 11:07am UTC](https://www.ruby-forum.com/t/how-to-test-several-locales-in-functional-tests/191620/8 "2010-07-19T11:07:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MickaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MickaSSSSl](https://www.ruby-forum.com/u/MickaSSSSl)\
**Post date:** [July 19, 2010, 2:52pm UTC](https://www.ruby-forum.com/t/how-to-test-several-locales-in-functional-tests/191620/9 "2010-07-19T14:52:35Z")

</div>

You’re welcome Christian.

To check every functional tests with available locales. We can do like  
my Gist [gist:481319 · GitHub](http://gist.github.com/481319)

Best regards,

Mickaël.
