0

I'm trying to follow the ruby on rails tutorial and impliment the Integration Testing.

I first run the command: bundle exec rspec spec/

And it tells me all but one of my sixteen tests passes. Here is the part where I think the issue is:

 require 'spec_helper'
 describe "LayoutLinks" do

   it "should have the right links on the layout" do
    visit root_path
    click_link "Help"
    response.should have_selector('title', :content => "Help")
    click_link "Contact"
    response.should have_selector('title', :content => "Contact")
    click_link "Home"
    response.should have_selector('title', :content => "Home")
    click_link "Sign up now!"
    response.should have_selector('title', :content => "Sign Up")
    click_link "About"
    response.should have_selector('title', :content => "About")
  end
end

As a result I get the following:

Failure/Error: response.should have_selector('title', :content => "Help")
   expected following output to contain a <title>Help</title> tag:
#home.html.erb page's source shown
<!DOCTYPE html>
   <html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
   <title>Ruby on Rails Tutorial Sample App | Home</title> 
   #The line above is why the test fails. 
   #It is loading home.html.erb instead of help.html.erb
   .
   .
   .
   # ./spec/requests/layout_links_spec.rb:34:in `block (2 levels) in <top (required)>'

I can move around the order of the tests and it is always the top test that fails. This makes me believe there is something wrong here and not with the actual rails code. I can also go to the demo website and the links work and they go to the correct pages. I have looked at the other issues other people have had with this and I can't seem to find anyone who is having the same issues. How can I go about trouble shooting this?

Update:

noahc:sample_app noahc$ rake routes
users_new GET /users/new(.:format)     {:controller=>"users", :action=>"new"}
   signup     /signup(.:format)        {:controller=>"users", :action=>"new"}
  contact     /contact(.:format)       {:controller=>"pages", :action=>"contact"}
    about     /about(.:format)         {:controller=>"pages", :action=>"about"}
     help     /help(.:format)          {:controller=>"pages", :action=>"help"}
              /help(.:format)          {:controller=>"pages", :action=>"help"}
     root     /                        {:controller=>"pages", :action=>"home"}
pages_home GET /pages/home(.:format)    {:controller=>"pages", :action=>"home"}
pages_contact GET /pages/contact(.:format) {:controller=>"pages", :action=>"contact"}
pages_about GET /pages/about(.:format)   {:controller=>"pages", :action=>"about"}
pages_help GET /pages/help(.:format)    {:controller=>"pages", :action=>"help"}
5
  • 1
    (1) if this is a copy/paste, then your last "click_link" is mispelled as "lick_link" (which is a completely different sort of action :P), (2) just before the line that fails, add a step that says save_and_open_page. What it should do is open up a stripped-down (no formatting, no .CSS) page in your browser, to show you exactly what RSpec is seeing, which might shed some light on the situation. Commented Oct 18, 2011 at 13:39
  • Thanks. There is no more link licking! However, I put the save_and_open_page after the click_link "Help" line and nothing happened. That is the correct place, right? Commented Oct 18, 2011 at 13:48
  • 1
    Oh, I'm sorry - I'm confusing it with Capybara, which is what does the save_and_open_page thing: jeffkreeftmeijer.com/2010/… Commented Oct 18, 2011 at 13:51
  • 1
    Is there any output below #home.html.erb page's source shown that you didn't include? Commented Oct 18, 2011 at 13:53
  • Yes, I've added it to the source code above. Commented Oct 18, 2011 at 14:02

2 Answers 2

1

Noah, I would recommend splitting that test up into multiple tests, one for each title that you would like to test. That will make it a little easier for you to pinpoint exactly what's wrong. Something like this:

it "should have a link to 'Help'" do
  visit root_path
  response.should have_selector('a', :href => help_path, :content => "Help")
  click_link "Help"
  response.should have_selector('title', :content => 'Help')
end

it "should have a link to 'Contact'" do
  visit root_path
  response.should have_selector('a', :href => contact_path, :content => "Contact")
  click_link "Help"
  response.should have_selector('title', :content => 'Contact')
end

etc... This will make it easier to pinpoint exactly what is going on, and where you're experiencing a problem. Also, what did it say was being displayed in the page's source? You cut off the error message before it explained what really happened in your paste snippet... Look inside what the code that is actually being returned says to see what the title is and how/why it is differing from what rspec is expecting.

Sign up to request clarification or add additional context in comments.

4 Comments

I've split them out and they each fail on the following line: response.should have_selector('header a', :href => help_path, :content => "Help")
This is how I wrote the tests when I was going through the RoR tutorial, but you're code may be slightly different. It sounds to me, as though the line for click_link is not working because it isn't finding the links. Are you sure that the links are rendering in the header of the pages? Perhaps you should try something more basic like response.should have_selector('a', :href => help_path, :content => "Help"). Also make sure that in your case that you have a route called help_path (run rake routes in a shell to see what routes you have), and also that the link's content is 'Help'.
I've figured it out. My templates are putting in <li><a href="contact">Contact</a></li> and my tests expect <li><a href="/contact">Contact</a></li> .
Noah, try updating your test code to use the code that I've posted here exactly as is posted now, and if it still fails on the response.should have_selector('a', :href=> help_path, :content => "Help"), then it means that you're page at root_path doesn't have a link to the help page anywhere on it. You need to ensure that somewhere in your layout dir or on your pages/home.html.erb that you have code like this <%= link_to "Help", :help %>. Otherwise there is no way that Webrat can click on the link, since it doesn't exist on the page.
1

I don't use rspec, but could be because "Home" != "Ruby on Rails Tutorial Sample App | Home"

The contents do not match at all, so the test fails.

Also, check your help link in your source to be sure it is in fact going where you think it is...

1 Comment

It doesn't do exact matching, but it checks for if it contains.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.