David
David

Reputation: 51

RSpec spec fail and I can't figure it out

I hate to be a help-vampire, but I'm new and I've been looking at this almost two days.

I'm working on Ruby on Rails Tutorial by Michael Hartl. I'm running the test in listing 4.4, which I will include below. The specific test that I'm trying to get to pass checks to see if the page has a selector with the text, "Ruby on Rails Tutorial Sample App". I'll probably find the answer to this problem during the process of asking it on SO, but here we go. Below are, what I believe, the relevant files:

/spec/requests/static_pages_spec.rb

require 'spec_helper'

describe "StaticPages" 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 
  ...[other tests for other pages, let me know if you think they're necessary]

end

app/helpers/application_helper.rb

module ApplicationHelper
    # It is a good idea to put controller specific helpers in their 
    # related helper file (e.g., helpers specifically for the 
    #   StaticPages controller). 

    # Returns the full title on a per-page basis:
    #   This helper will be used for each page's title (unless
    #   otherwise noted).
    def full_title(page_title)
        base_title = "Ruby on Rails Tutorial Sample App"
        if page_title.empty?
            base_title
        else
            "#{base_title} | #{page_title}"
        end
    end

end

app/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title><%= full_title(yield(:title))%></title>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

app/views/static_pages/home.html.erb

<% provide :title, "Home" %>
<h1>Sample App</h1>
<p>
    This is the home page for a
    <a href="http://railstutorial.org/">Ruby on Rails</a> tutorial.
</p>
<p>
    It's by <a href="https://github.com/mhartl">Michael Hartl</a>
</p>

I have not configured Sublime to run rspec in the console, yet. When I run the test in Terminal from a bash, I get the following:

.......F.

Failures:

  1) StaticPages Home page should have the base title
     Failure/Error: page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App")
       expected #has_selector?("title", {:text=>"Ruby on Rails Tutorial Sample App"}) to return true, got false
     # ./spec/requests/static_pages_spec.rb:14:in `block (3 levels) in <top (required)>'

Finished in 0.14273 seconds
9 examples, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:12 # StaticPages Home page should have the base title

Randomized with seed #####

When I look at the source code in chrome, I can clearly see a pair of tags with "Ruby on Rails Tutorial Sample App | Home" in between them.

Well, I've gone through it all and I can't figure out what's going on. Hello, SO world! Please help. Thank you.

I would push to github right now, but they're down. I'll update when possible.

Upvotes: 0

Views: 324

Answers (1)

haley
haley

Reputation: 1593

change

have_selector('title', :text => "Ruby on Rails Tutorial Sample App")

to

have_title("Ruby on Rails Tutorial Sample App | Home")

It's a change to Capybara and was updated in Michael Hartl's tutorial. To see the update search for "Change have_selector(’title’, …) to have_title(…)" once in the tutorial.

Upvotes: 1

Related Questions