1

I'm plodding through Mike Hartl's Rails Tutorial and in section 4.4 it appears that it had me change the rspec request file from the format of:

page.should have_selector('title', :text => "#{base_title}")

to

expect(page).to have_title("Ruby on Rails Tutorial Sample App")

I now get two undefined method errors where I use ".to have_title" and ".to_not have_title". I shutdown and restarted Webrick, Spork, and Guard just in case, but it still doesn't work.

Capybara version 1.1.2 Rspec version 2.11.1

Please let me know if any other info is needed.

Haltingpoint
  • 101
  • 1
  • 9
  • This may assist, especially if you've upgraded to Capybara 2.1 as you indicated in the comments of an answer: [RSpec & Capybara 2.0 tripping up my have_selector tests](http://stackoverflow.com/q/13573525/567863) – Paul Fioravanti May 27 '13 at 12:39

3 Answers3

4

Apparently the tutorial has changed recently.

Accessing the page through google cache reveals the original version (which works fine for me):

require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

    it "should have the h1 'Sample App'" do
      visit '/static_pages/home'
      page.should have_selector('h1', :text => 'Sample App')
    end

    it "should have the base title" do
      visit '/static_pages/home'
      page.should have_selector('title',
                        :text => "Ruby on Rails Tutorial Sample App")
    end

    it "should not have a custom page title" do
      visit '/static_pages/home'
      page.should_not have_selector('title', :text => '| Home')
    end
  end
  .
  .
  .
end
chameleon
  • 56
  • 1
  • So this definitely did the trick, and when I recently reloaded the page for section 4.4, it actually had your corrected version that you pasted in here. Did Mike Hartl fix it since yesterday or something? I feel like I'm going a little crazy... – Haltingpoint May 28 '13 at 09:31
  • Also, just one note that the code for the top of the static_pages_spec.rb file is missing `let(:base_title) { "Ruby on Rails Tutorial Sample App" }` between the `describe "Static pages" do` and `describe "Home page" do`. – Haltingpoint May 28 '13 at 09:32
0

May be the issue with your version. Go through https://github.com/jnicklas/capybara/issues/863

expect(first('title').native.text).to eq "my title"
Sachin R
  • 11,606
  • 10
  • 35
  • 40
0

I had this problem, I realised eventually I had switched to following the code for rails version 4, rather than 3.2.

to test what version of rails you are using, type the command

rails -v

Then on the tutorial page, use the navigator on the right to select the right rails version. Seems simple, but hopefully it will save grief for others with this issue.

Pete McFarlane
  • 431
  • 3
  • 5