views:

39

answers:

4

I'm running through Michael Hartl's Rails Tutorial.

I'm trying to verify the title of my page. The test looks like this:

it "should have the right title" do
      get 'home'
      response.should have_selector("title", :content => "Ruby on Rails Tutorial Sample App | Home")
    end

The HTML head section looks like this

<head>
    <title>Ruby on Rails Tutorial Sample App | Home</title>
</head>

I'm getting the following failure

1) PagesController GET 'home' should have the right title Failure/Error: response.should have_selector("title", :content => "Ruby on Rails Tutorial Sample App | Home") expected following output to contain a Ruby on Rails Tutorial Sample App | Home tag: # ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in '

I'm expecting this to pass. What am I doing wrong? I'm using Rails 3 and RSpec 2.0.0

A: 

Alternatively, you can use the testing framework that is build into Rails. Check out the Rails Guide for details:

get :home
assert_select "title", "Ruby on Rails Tutorial Sample App | Home"
balexand
I am using Rails 3 (sorry, should have mentioned that). I tried your example, but it didn't work for me.
Jaco Pretorius
Sorry, my response doesn't apply to RSpec. I've updated it.
balexand
A: 

Controller specs don't normally render the complete view, since they're intended to test controllers in isolation. You can tell Rspec to render the whole page by including the directive integrate_views at the top of the example group:

describe MyController do
  integrate_views

However you should ask yourself if you really want to do this, or if it would make more sense to write view specs.

btw you can also use the CSS3 selector syntax to save a few keystrokes (might need to include the Webrat matchers for this):

response.should have_selector("title:contains('Ruby on Rails Tutorial Sample App | Home')")

EDIT

For Rspec2, replace integrate_views with render_views

zetetic
I tried putting the integrate_views directive at the top, but now I'm getting a syntax error
Jaco Pretorius
Oops, you must be on Rspec 2. Try `render_views`.
zetetic
Thanks, maybe just edit your answer to say you should use render_views on Rspec 2?
Jaco Pretorius
A: 

I strongly suspect you have a typo somewhere.

As of this instant, I'm working through Exercise 4 of Chapter 11. Every, single problem I've had with the tutorial turned out to be a typo on my part.

Note: I'm not cutting and pasting. I'm punching in all the code by hand.

Dave Doolin
I did the copy-paste thing... And it worked perfectly once I put render_views at the top
Jaco Pretorius
A: 
I found it easier to move to capybara (I'm using Rails 3.0.1, Rspec 2.0.1, Ruby 1.9.2), then you can do something like this:

page.should have_css('title', :text => 'Ruby on Rails Tutorial Sample App | Home')
Tim