我正在阅读 Michael Hartl 的 Ruby on Rails 教程并遇到了这个问题。我得到了以下错误,而不是通常的 rspec 错误来返回某些东西。
静态页面,帮助页面
Failure/Error: it { should_not have_selector 'title', text: "| Help" }
expected css "title" with text "| Help" not to return anything
# ./spec/requests/static_pages_spec.rb:27:in `block (3 levels) in <top (required)>'
我的 rspec static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
subject {page}
shared_examples_for "all static pages" do
it { should have_selector('h1', text: heading)}
it { should have_selector('title', text: full_title(page_title))}
end
describe "Home page" do
before { visit root_path }
let(:heading) {'Welcome to TechnoEdge'}
let(:page_title) {''}
it_should_behave_like "all static pages"
it { should_not have_selector 'title', text: '| Home' }
end
describe "Help page" do
before { visit help_path }
let(:heading) {'Help'}
let(:page_title) {'Help'}
it_should_behave_like "all static pages"
it { should_not have_selector 'title', text: "| Help" }
end
describe "About page" do
before { visit about_path }
let(:heading) {'About Us'}
let(:page_title) {'About Us'}
it_should_behave_like "all static pages"
it { should_not have_selector 'title', text: '| About us' }
end
describe "Contact page" do
before { visit contact_path }
let(:heading) {'Contact'}
let(:page_title) {'Contact'}
it_should_behave_like "all static pages"
it { should_not have_selector 'title', text: '| Contact page' }
end
end
主页.html.erb
<% provide(:title, 'Help') %>
<h1>Help</h1>
<p>This is the Help page</p>
应用程序.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</body>
</html>