0

我想通过 rspec 检查页面的标题。在 chrome 中我看到了预期的结果,但 rspec 声称标题为空(在 chrome 视图源中它也可以)。这里有一些代码:

应用程序.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>

主页.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

路线.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

和错误

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

编辑: 这是我的 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
4

2 回答 2

0

我看到问题是关于会话的。Capybara 在每个itscenario您的情况下过期会话。

因此,您visit page在第一种情况下被消耗掉了,然后在第二种情况下没有食物可吃。

您可以尝试:

  1. 删除before { visit '/home_page/home'}部件,并在每个场景中访问页面。
  2. 将这两种情况合二为一。但是,仍然visit是首选放在里面。
于 2013-02-27T17:33:23.397 回答
0

代替

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

我把它改成

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

它有效,我仔细检查了它。如果有人理解为什么单独的页面不起作用,请分享。

于 2013-02-28T16:34:15.967 回答