I've been battling with assert_select in my integration tests for several evenings now. I'm testing a multi-step form, and after submitting the first step, my assert_selects fail on the attempts to find the form on the next page. However if I print out the response object, it has the exact text of what I'm trying to match.
Here is the complete integration test. The failure occures on the first assert_select in the second call to click_button.
require "#{File.dirname(__FILE__)}/../test_helper"
class AccountCreationTest < ActionController::IntegrationTest
def test_personal_account_creation goto_home_page click_link "Create Account", "/account/new_account_step_one" click_button "Next Step", "/account/new_account_step_one", :user=>{:username=>"TestUser", :password=>'T3st!ng', :password_confirmation=>'T3st!ng', :account_type=>'Personal', :email=>'me@example.net'} click_button "Create Account", "/account/new_account_step_two", {:first_name=>'Test', :last_name=>'User'} end
private def goto_home_page get "/" assert_response :success assert_template "register/list" end
def click_link(link_text, destination) assert_select "a[href*=#{destination}]", link_text get destination assert_response :success end
def click_button(button_text, destination, params={}) assert_select "form[action=#{destination}]" do assert_select "input[type=submit][value=#{button_text}]" end post_via_redirect destination, params assert_response :success end
end
Here is the failure report: 1) Failure: test_personal_account_creation(AccountCreationTest) [/Users/nielsene/ruby-work/compinabox/config/../vendor/rails/actionpack/lib/action_controller/assertions/selector_assertions.rb:281:in `assert_select' test/integration/account_creation_test.rb:37:in `click_button' test/integration/account_creation_test.rb:11:in `test_personal_account_creation' /Users/nielsene/ruby-work/compinabox/config/../vendor/rails/actionpack/lib/action_controller/integration.rb:453:in `run']: Expected at least 1 elements, found 0. <false> is not true.
And here is an except from "puts response.inspect" #<ActionController::CgiResponse:0x247432c @redirected_to=nil, @headers={"Status"=>"200 OK", "type"=>"text/html; charset=utf-8", "cookie"=>, "Cache-Control"=>"no-cache", "Content-Length"=>2247}, @session=#<CGI::Session:0x246e2b0 @session_id="d0060c4ee33f3d58c0ef83bbfa107b6f", @data={:pending_user=>{"password_confirmation"=>"T3st!ng", "username"=>"TestUser", "account_type"=>"Personal", "password"=>"T3st!ng", "email"=>"me@example.net"}, "flash"=>{}}, @dbprot=, @dbman=#<CGI::Session::ActiveRecordStore:0x246d004 @session=nil>, @new_session=false>, @cgi=#<ActionController::Integration::Session::MockCGI:0x24772fc @output_cookies=[["d0060c4ee33f3d58c0ef83bbfa107b6f"]], @env_table={"CONTENT_LENGTH"=>nil, "HTTP_HOST"=>"www.example.com", "REMOTE_ADDR"=>"127.0.0.1", "CONTENT_TYPE"=>"application/x-www-form-urlencoded", "HTTPS"=>"off", "HTTP_COOKIE"=>"_session_id=d0060c4ee33f3d58c0ef83bbfa107b6f; ", "REQUEST_URI"=>"/account/new_account_step_two", "SERVER_PORT"=>"80", "QUERY_STRING"=>nil, "HTTP_ACCEPT"=>"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "REQUEST_METHOD"=>"GET"}, @output_hidden={"_session_id"=>"d0060c4ee33f3d58c0ef83bbfa107b6f"}, @stdoutput=#<StringIO:0x24771d0>, @cookies={"_session_id"=>["d0060c4ee33f3d58c0ef83bbfa107b6f"]}, @stdinput=#<StringIO:0x24771e4>, @params={}, @multipart=false>, @body=" [ header of page cut .... ] <div id=\"main-double\">\n\t\t\t\n\n<form action=\"/account/new_account_step_two\" method=\"post\"><p><label for=\"user_first_name\">First Name:</label><br/>\n\t<input id=\"first_name\" name=\"first_name\" type=\"text\" /></p>\n<p><label for=\"user_last_name\">Last Name:</label><br/>\n\t<input id=\"last_name\" name=\"last_name\" type=\"text\" /></p>\n<input name=\"commit\" type=\"submit\" value=\"Create Account\" /></form>\n\t\t</div>\n\t</div>\n \t<div class=\"clearing\"> </div>\n</div> .....
Can anyone help me understand what I've been doing wrong?