Topic: Testing for url

Hello.

I've just started dabbling with TDD. What I want to write is this:

assert_select "a[href=#{url_for :action => :new}]", nil, "Should have link to Create Account page"

This obviously does not work, because the url_for method is not available in the test environment. Could anyone clue me into how this could be done in a good way?

Thanks :-)

-Magnar

Last edited by Magnar (2007-05-29 04:05:16)

Re: Testing for url

Hmm, I thought url_for was a vailable in the functional tests? I know the named route methods are. What error are you getting?

Railscasts - Free Ruby on Rails Screencasts

Re: Testing for url

Thanks for the quick reply! Here's the error message:

  1) Error:
test_should_get_index(AccountsControllerTest):
NoMethodError: undefined method `url_for' for #<AccountsControllerTest:0x475f9b4>
    C:/rubyonrails/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/test_process.rb:452:in `method_missing'
    ./test/functional/accounts_controller_test.rb:20:in `test_should_get_index'

2 tests, 4 assertions, 0 failures, 1 errors


And here's the entire functional test file:
require File.dirname(__FILE__) + '/../test_helper'
require 'accounts_controller'

# Re-raise errors caught by the controller.
class AccountsController; def rescue_action(e) raise e end; end

class AccountsControllerTest < Test::Unit::TestCase
  fixtures :accounts
 
  def setup
    @controller = AccountsController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end

  def test_should_get_index
    get :index
    assert_response :success
    assert assigns(:accounts), "@accounts should be assinged"
    assert_select "a[href=?]", url_for(:action => "new"), "Should have link to Create Account page"
  end
   
end


Are you able to make any sense of it? Thanks for helping!

Re: Testing for url

Hmm, it does seem url_for isn't available, but the URL rewriter is. See this blog post on how you can add url_for.

Railscasts - Free Ruby on Rails Screencasts

Re: Testing for url

Wonderful, that worked beautifully. Thanks alot for your help, Ryan. :-)