Search code examples
ruby-on-railstestingrspecassert

With Rails and Rspec, is there a way to see what page an action really takes you to?


I have a rails test in which I go to a page and enter information, save the page (which takes me back one page), and attempt to come back to see if the data is still there. I am hitting a wall, but suspect that if I could see what is on the page, I could discern what when wrong or how to adapt my assertions. Is there a way to visualize/breakpoint/curl that page that test is actually encountering? Everything I assert is coming back false.

it "Emergency Contact step saves data submitted" do
  click_link "Register online"
  click_link "Emergency Contacts"
  fill_in "Contact 1 Name", :with => "1 name"
  fill_in "Contact 1 Phone", :with => "123.456.7890"
  click_button "Save"
  click_link "Emergency Contacts"
  assert page.has_content?("1 name")
  assert page.has_content?("123.456.7890") 
end

Solution

  • One technique I use after a page transition is to check the current path (route) against a known one like this:

    assert_equal page.current_path, new_page_path
    # or
    assert_equal page.current_path, "/things/new"
    

    Another way to debug could be to look at the contents of page.html like

    puts page.html
    

    Or use capybara's save_and_open_page method, which save the HTML of the current page to your Rails app's tmp directory and opens that in a browser.