Why does rspec fail?

737 Views Asked by At

i want to check page's title through rspec. in chrome i see the expected result but rspec claim the title is empty(in chrome view source it also ok). here some code:

application.html.rb

<!DOCTYPE html>
<html>
<head>
  <title>site | <%= yield(:title) %></title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

home.html.erb

<% provide(:title, 'Home') %>
<h1><%= yield(:title) %></h1>
<article>
  <div><a href="#">Cameras</a></div>
  <div><a href="#">TVs</a></div>
  <div><a href="#">Laptops</a></div>
</article>

home_page_controller.rb

class HomePageController < ApplicationController
  def home
  end
end

routes.rb

Reviewsite::Application.routes.draw do
   get "home_page/home"

home_pages_spec.rb

require 'spec_helper'
feature "HomePages" do

  before { visit '/home_page/home'}

  scenario do
    expect(page).to have_selector('h1', text: "Home")
  end

  scenario do
    expect(page).to have_selector('title', text: "site | Home")
  end

end

and the error

Failures:

  1) HomePages 
     Failure/Error: expect(page).to have_selector('title', text: "site | Home")
     Capybara::ExpectationNotMet:
       expected to find css "title" with text "site | Home" but there were no matches. Also found "", which matched the selector but not all filters.
     # ./spec/features/home_pages_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 2.52 seconds
2 examples, 1 failure

EDIT: here is my gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.12'
gem 'bootstrap-sass', '2.3.0.1'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
gem 'jquery-rails', '2.2.1'
gem 'mysql2'
#gem 'execjs'
gem 'therubyracer', :platforms => :ruby

# Dev and test gems
group :development, :test do
  gem 'rspec-rails', '2.13.0'
  gem 'guard-rspec', '2.4.1'
  gem 'guard-spork', '1.4.2'
  gem 'spork', '0.9.2'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '3.2.6'
  gem 'coffee-rails', '3.2.2'
  gem 'uglifier', '1.3.0'
end

#only test gems
group :test do
  gem 'capybara', '2.0.2'
  gem 'factory_girl_rails', '4.2.1'
  gem 'cucumber-rails', '1.3.0', :require => false
  gem 'database_cleaner', '0.9.1'
  # gem 'launchy', '2.1.0'
  # gem 'rb-fsevent', '0.9.1', :require => false
  # gem 'growl', '1.0.3'
end

group :production do
  gem 'pg', '0.12.2'
end
2

There are 2 best solutions below

0
On BEST ANSWER

instead of

expect(page).to have_selector('title', text: "site | Home")

i changed it to

expect(page.html).to have_selector('title', text: "site | Home")

and it worked, i double checked it. if anyone understand why page alone doesnt work, please share.

7
On

I see the problem is about session. Capybara expire session in each it or scenario in your case.

So your visit page is consumed in first scenario, then the second one has no food to eat.

You can try either:

  1. Remove the before { visit '/home_page/home'} part, and visit the page in every scenario.
  2. Combine this two scenario to one. But, still, visit is preferred to put inside it.