Search code examples
ruby-on-railsseleniumintegration-testingwebrat

assert_response doesn't work with webrat in selenium mode


Anyone know how to get assert_response to work with webrat using selenium? I keep getting

NoMethodError: undefined method `response_code' for nil:NilClass

Here's my test:

def test_basic_page_load
  visit root_path
  click_link "register"
  assert_response 200
end

Here's the error:

==> Waiting for Selenium RC server on port 4444... Ready!
==> Waiting for rails application server on port 3001... Ready!
E

  1) Error:
test_basic_page_load(UserCrudTest):
NoMethodError: undefined method `response_code' for nil:NilClass
    /test/integration/user_crud_test.rb:11:in `test_basic_page_load'

Finished in 12.269964 seconds.

I'm sure it's something silly that I'm missing, but I just don't see it.... Any ideas?


Solution

  • Hmmm, it looks like it's not possible at the moment.

    The testing process starts a new mongrel instance from the command line (in Webrat::Selenium::ApplicationServers::Rails) and therefore doesn't have access to the @controller variable that would be set by ActionController::Integration::Session.get. This @controller variable is used by assert_response.

    I wonder how much work it would be to start up a mongrel server using fork instead of system (in Webrat::Selenium::ApplicationServers::Rails.start). It could maybe be combined with selenium.wait_for_page_to_load to ensure the test waits for the server process to finish the response. Just an idea, I probably won't look into it any more.

    For anyone using webrat with shoulda, something like this will still work:

    setup do
      fill_in :login, :with => Factory.next(:login)
      fill_in :email, :with => Factory.next(:email)
      fill_in "Password", :with => 'asdf'
      fill_in "Password confirmation", :with => 'asdf'
      click_button :Register
      selenium.wait_for_page_to_load(10)
    end
    should_create :user
    

    Cheers, Brian