I have a test case that tests a layout. It looks to see if certain links are present or absent for different kinds of users (admin links, for instance). This worked fine until we started to localize some of our interface.
describe "layouts/application" do
include Devise::TestHelpers
fixtures :users
it "renders admin link for admin user" do
@admin = users(:admin)
sign_in(@admin)
@locale = 'en'
render
rendered.should match(/Administration/)
end
end
The layout now contains a call开发者_Go百科 to <%= toggle_language %>, which uses url_for to generate a URL like the current one, but with the :locale parameter changed:
def toggle_language
case locale
when :fr
other=:en
when :en
other=:fr
else
other=:fr
end
link_to t(:other_language), url_for(:locale => other)
end
The problem is that since I have no controller called "layouts", the url_for can't generate what it needs.
1) layouts/application renders admin link for admin user
Failure/Error: render
ActionView::Template::Error:
No route matches {:controller=>"layouts", :locale=>:fr, :action=>"application"}
I've looked to see if perhaps the rspec render command would take a :controller,:action, which I could point at a valid route. I haven't found such a thing yet.
Yes, stubbing this out works very well.
精彩评论