我想通过rspec检查页面的标题。在chrome我看到了预期的结果但是rspec声称标题是空的(在chrome视图源中它也可以)。这里有一些代码:
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
和错误
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
修改 这是我的宝石文件
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
答案 0 :(得分:0)
我发现问题与会话有关。在您的案例中,Capybara会在每个it
或scenario
过期。
所以你的visit page
在第一个场景中被消耗,然后第二个没有食物。
你可以尝试:
before { visit '/home_page/home'}
部分,然后在每个方案中访问该页面。visit
放入其中。答案 1 :(得分:0)
而不是
expect(page).to have_selector('title', text: "site | Home")
我把它改成了
expect(page.html).to have_selector('title', text: "site | Home")
它有效,我仔细检查了一下。 如果有人理解为什么单独的页面不起作用,请分享。