0
votes

I am following Ruby on rails tutorial by Michael Hartl.

Refactoring a simple test using rspec is failing it, I will paste first what works, then i will paste what works ....

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 title 'Home'" do
      visit '/static_pages/home'
      page.should have_selector('title',
                        :text => "Ruby on Rails Tutorial Sample App | Home")
    end
   end

    describe "Help page" do

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

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

Now I will paste what fails in rspec

require 'spec_helper'

describe "Static pages" do

  let(:base_title) { "Ruby on Rails Tutorial Sample App |" }

  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 title 'Home'" do
      visit '/static_pages/home'
      page.should have_selector('title', :text => "#{base_title}  Home")
    end
  end



  describe "Help page" do

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

    it "should have the title 'Help'" do
      visit '/static_pages/help'
      page.should have_selector('title', :text => "#{base_title}  Help")
    end
  end 
end

Also i am new to this so i would like to ask what more information might be required ... the change is obviously

  let(:base_title) { "Ruby on Rails Tutorial Sample App |" }

Thanks for any help !

Failing error being ....

Failures:

1) Static pages Home page should have the title 'Home' Failure/Error: page.should have_selector('title', :text => "#base_title} Home") expected css "title" with text "#base_title} Home" to return something # ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in '

2) Static pages Help page should have the title 'Help' Failure/Error: page.should have_selector('title', :text => "#{base_title} Help") expected css "title" with text "Ruby on Rails Tutorial Sample App | Help" to return something # ./spec/requests/static_pages_spec.rb:29:in `block (3 levels) in '

3
It seems like you're doing Michael Hartl's Rails Tutorial, which means you are likely using Github. Rather than post error output in comments to answers that didn't work for you, you would probably help yourself by editing your question to add the address to your repo and people can clone it and run your tests for themselves.Paul Fioravanti

3 Answers

1
votes

It has been a while since this question was asked. If it's still relevant:

There are two spaces after #{base_title}.

Delete a space and it should work.

0
votes

it looks like you missed a { in the first example:

 "title" with text "#base_title} Home"

this string is just not interpolated and has nothing to do with let

and other error should not occur if your code and test work properly.

0
votes

Just be ahead of changing Capybara and Webrat syntaxes. This should work for your example:

 page.should have_selector('h1', text: "#{base_title}  Help")

or you can replace it with class like that:

page.should have_selector("h1[class='title']", text: "#{base_title} Help)