This project is archived and is in readonly mode.
ActionView::TestCase does not honor default_url_options set in application_controller.rb
Reported by AdamTao | November 17th, 2010 @ 10:33 PM | in 3.0.5
Using Rails 3, I have a :scope in routes.rb:
scope "(/:locale)" do
resources :products
end
In application_controller.rb I set the default :locale
def default_url_options(options={})
{:locale => "en-US"}
end
In my views, this works when I test in my browser, but ActionView::TestCase tests fail
<%= link_to product.name, product %>
While running a test, I see:
No route matches {:action=>"show", :controller=>"products", :locale=>#<Product id: ...>}
Here is a test that fails:
class ProductShowTest < ActionView::TestCase
def test_render
render "products/show", :product => Product.new(:name => "WidgetA")
assert_match /WidgetA/, rendered
end
end
Comments and changes to this ticket
-
Andrew White November 18th, 2010 @ 07:06 AM
- State changed from new to open
- Milestone cleared.
- Assigned user set to Andrew White
- Importance changed from to Low
-
Andrew White February 14th, 2011 @ 10:06 AM
- State changed from open to invalid
When unit testing views the controller is just a stub controller which inherits from ActionController::Base and not ApplicationController. You need to override this stub if you need special behaviour like default_url_options, e.g:
# app/views/products/index.html.erb <%= link_to('Products', products_path) %> # test/unit/views/products_index_test.rb require 'test_helper' class ProductsIndexTest < ActionView::TestCase class ProductsController < TestController def default_url_options { :locale => 'de' } end end def setup @controller = ProductsController.new end def test_render render :template => "products/index" assert_match %r{href="/de/products"}, rendered end end
Note that you have to define the controller in your test as using the real one will not have a test request and response.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>