[rspec-users] spec with user culture

0 views
Skip to first unread message

Gnagno Gnagno

unread,
Dec 22, 2009, 6:26:23 AM12/22/09
to rspec...@rubyforge.org
Hello all,
I am making my first experiments with rspec, I wanted to do something
like this:

when a user visit the home page of my site he will be redirected
depending on his culture, so if his culture is english he will be
redirected to myapp/en if he is italian to myapp/it and so on....

how can I say this with rspec?
I was trying something like:

it "should redirect to spanish home page" do
get 'index'
#don't know how to say the culture is spanish
response.should redirect_to(spanish_home_page)
end

any help?

Thanks
Gnagno
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Tom Stuart

unread,
Dec 22, 2009, 6:41:01 AM12/22/09
to rspec...@rubyforge.org
On 22 Dec 2009, at 11:26, Gnagno Gnagno wrote:
> it "should redirect to spanish home page" do
> get 'index'
> #don't know how to say the culture is spanish
> response.should redirect_to(spanish_home_page)
> end

How does the application detect the user's "culture"?

Cheers,
-Tom

Gnagno Gnagno

unread,
Dec 22, 2009, 9:25:12 AM12/22/09
to rspec...@rubyforge.org
Tom Stuart wrote:
> How does the application detect the user's "culture"?
>
> Cheers,
> -Tom

Thanks for your reply Tom,

in my home controller I have a line like this for each language:
redirect_to localized_home_page_path :culture => 'es' and return if
request.env['HTTP_ACCEPT_LANGUAGE'].include? 'es-ES'

as I said before I am just 'experimenting and playing' so any suggestion
is accepted :)


maybe I should access to the request object from my spec code?

David Chelimsky

unread,
Dec 22, 2009, 9:33:32 AM12/22/09
to rspec...@rubyforge.org
On Tue, Dec 22, 2009 at 8:25 AM, Gnagno Gnagno <li...@ruby-forum.com> wrote:
Tom Stuart wrote:
> How does the application detect the user's "culture"?
>
> Cheers,
> -Tom

Thanks for your reply Tom,

in my home controller I have a line like this for each language:
redirect_to localized_home_page_path :culture => 'es' and return if
request.env['HTTP_ACCEPT_LANGUAGE'].include? 'es-ES'

You can set that explicitly in the example:

it "should redirect to spanish home page" do
  request.env['HTTP_ACCEPT_LANGUAGE'] = 'es-ES'
  get 'index'
  response.should redirect_to(spanish_home_page)
end

HTH,
David

Gnagno Gnagno

unread,
Dec 23, 2009, 5:01:59 AM12/23/09
to rspec...@rubyforge.org
Thank you very much :)
I didn't know I could access request.env['HTTP_ACCEPT_LANGUAGE'] in
writing, and didn't even try it

Gnagno Gnagno

unread,
Dec 23, 2009, 5:07:38 AM12/23/09
to rspec...@rubyforge.org
Sorry, I have one more question,

I didn't find the cucumber forum, so please forgive me if I am too much
out of topic here.

I was trying to achieve the same with cucumber, so I wrote this:

Scenario Outline: visit home page and get redirect to localized home
page
Given my culture is <culture>
When I go to the home page
Then I should be redirected to the <page>

Examples:
| culture | page |
| italian | italian_home_page |
| english | english_home_page |
| french | french_home_page |
| spanish | spanish_home_page |
| german | german_home_page |
| japanese | japanese_home_page |

but I cannot access request.env from a cucumber step, and moreover I
think accessing to request.env from cucumber could tie it too much to
the application, am I right?

Matt Wynne

unread,
Dec 23, 2009, 5:14:47 AM12/23/09
to rspec...@rubyforge.org

On 23 Dec 2009, at 10:07, Gnagno Gnagno wrote:

> Sorry, I have one more question,
>
> I didn't find the cucumber forum, so please forgive me if I am too
> much
> out of topic here.

http://wiki.github.com/aslakhellesoy/cucumber/get-in-touch

> I was trying to achieve the same with cucumber, so I wrote this:
>
> Scenario Outline: visit home page and get redirect to localized home
> page
> Given my culture is <culture>
> When I go to the home page
> Then I should be redirected to the <page>
>
> Examples:
> | culture | page |
> | italian | italian_home_page |
> | english | english_home_page |
> | french | french_home_page |
> | spanish | spanish_home_page |
> | german | german_home_page |
> | japanese | japanese_home_page |
>
> but I cannot access request.env from a cucumber step, and moreover I
> think accessing to request.env from cucumber could tie it too much to
> the application, am I right?

Right. You need to ask the cukes group (or the webrat group, or the
rails group) about how you pass HTTP headers with your in the step
"When I go to the home page".

>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec...@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

http://mattwynne.net
+447974 430184

Gnagno Gnagno

unread,
Dec 23, 2009, 7:34:53 AM12/23/09
to rspec...@rubyforge.org
I solved the question concerning cucumber, I will post here the solution
in case someone else will need it:

in my step definitions I just put:

Given /^my culture is (.+)$/ do |culture|
header "HTTP_ACCEPT_LANGUAGE", "it-IT" if culture == 'italian'
header "HTTP_ACCEPT_LANGUAGE", "en-GB" if culture == 'english'
header "HTTP_ACCEPT_LANGUAGE", "fr-FR" if culture == 'french'
header "HTTP_ACCEPT_LANGUAGE", "es-ES" if culture == 'spanish'
header "HTTP_ACCEPT_LANGUAGE", "de-DE" if culture == 'german'
header "HTTP_ACCEPT_LANGUAGE", "en-US" if culture == 'american'
header "HTTP_ACCEPT_LANGUAGE", "jp-JP" if culture == 'japanese'
end

and it works :)

Reply all
Reply to author
Forward
0 new messages