0

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
Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
Nir
  • 2,497
  • 9
  • 42
  • 71
  • possible duplicate of [Capybara: How to test the title of a page?](http://stackoverflow.com/questions/5129423/capybara-how-to-test-the-title-of-a-page) – Jared Beck Dec 06 '13 at 22:59

2 Answers2

0

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.
Billy Chan
  • 24,625
  • 4
  • 52
  • 68
  • i tried to put visit inside each scenario but it didnt work. plus the before command should be called each time before a scenario section, doesnt it? – Nir Feb 27 '13 at 17:47
  • Can you try rename scenario to `it`? Besides, before(:each) will be called each time. I have similar problem before, and fixed it by applying the method above. – Billy Chan Feb 27 '13 at 17:52
  • I was using `before(:each)` before and it still not worked. Here is a similar question http://stackoverflow.com/questions/9630170/capybara-selenium-with-rspec-before-all-hook – Billy Chan Feb 27 '13 at 17:55
  • it command didnt helped, plus i tried to combine this two scenario into one scenario and it still failed moreover just run the second scenario get failed too! – Nir Feb 27 '13 at 19:25
  • i've added the controller and routes.rb to my original question. – Nir Feb 28 '13 at 09:17
  • It's weird. I think the actual page may be blank and your first test got a false passed message. Have you ever tried to see it in development mode manually? – Billy Chan Feb 28 '13 at 09:30
  • in debug both scenarios show the good html if i check the html in the watch through page.html – Nir Feb 28 '13 at 10:15
0

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.

Nir
  • 2,497
  • 9
  • 42
  • 71