Ruby on rails rspec./spec/requests/static\u pages\u spec失败。迈克尔·哈特尔的第五章';导游
我不知道是什么导致了这些失败!这是我在stackoverflow上的第一篇帖子,如果我遗漏了一些需要的信息,我会提前道歉。。好吧,失败就在这里Ruby on rails rspec./spec/requests/static\u pages\u spec失败。迈克尔·哈特尔的第五章';导游,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我不知道是什么导致了这些失败!这是我在stackoverflow上的第一篇帖子,如果我遗漏了一些需要的信息,我会提前道歉。。好吧,失败就在这里 Failures: 1) Static pages Help page Failure/Error: it { should have_selector('title', text: full_title('Help')) } expected css "title" with text "Ruby on Rails Tutorial Sam
Failures:
1) Static pages Help page
Failure/Error: it { should have_selector('title', text: full_title('Help')) }
expected css "title" with text "Ruby on Rails Tutorial Sample App | Help" to return something
# ./spec/requests/static_pages_spec.rb:19:in `block (3 levels) in <top (required)>'
2) Static pages About page
Failure/Error: it { should have_selector('title', text: full_title('About Us')) }
expected css "title" with text "Ruby on Rails Tutorial Sample App | About Us" to return something
# ./spec/requests/static_pages_spec.rb:26:in `block (3 levels) in <top (required)>'
3) Static pages Contact page
Failure/Error: it { should have_selector('title', text: full_title('Contact')) }
expected css "title" with text "Ruby on Rails Tutorial Sample App | Contact" to return something
# ./spec/requests/static_pages_spec.rb:33:in `block (3 levels) in <top (required)>'
Finished in 0.35756 seconds
9 examples, 3 failures
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:19 # Static pages Help page
rspec ./spec/requests/static_pages_spec.rb:26 # Static pages About page
rspec ./spec/requests/static_pages_spec.rb:33 # Static pages Contact page
静态页面规格rb
require 'spec_helper'
describe "Static pages" do
subject { page }
describe "Home page" do
before { visit root_path }
it { should have_selector('h1', text: 'Sample App') }
it { should have_selector('title', text: full_title('')) }
it { should_not have_selector 'title', text: '| Home' }
end
describe "Help page" do
before { visit help_path }
it { should have_selector('h1', text: 'Help') }
it { should have_selector('title', text: full_title('Help')) }
end
describe "About page" do
before { visit about_path }
it { should have_selector('h1', text: 'About') }
it { should have_selector('title', text: full_title('About Us')) }
end
describe "Contact page" do
before { visit contact_path }
it { should have_selector('h1', text: 'Contact') }
it { should have_selector('title', text: full_title('Contact')) }
end
end
contact.html.erb
<h1>Contact</h1>
<p>
Contact Ruby on Rails Tutorial about the sample app at the
<a href="http://railstutorial.org/contact">contact page</a>.
</p>
联系人
有关示例应用程序的详细信息,请联系RubyonRails教程
.
application.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>
您的测试失败,因为您的应用程序布局正在生成标题,但您没有在任何地方设置标题。当您在视图中看到
yield:something
时,例如对于标题,这意味着视图将为“:something”呈现它知道的任何内容。在您的情况下,标题将通过以下方式显示:
<title><%= full_title(yield(:title)) %></title>
在每个视图中重复此操作可能会很乏味,因此许多人会为标题创建一个帮助器:
module ApplicationHelper
def title(page_title = '')
content_for(:title) { page_title }
end
end
在你看来,你可以做:
<%= title 'My Title' %>
查看此技术的更多信息您在contact.html.erb中没有关于:title的任何
内容,因此您的title标签不应包含您期望的内容。对不起,我没有跟踪您。请你再解释一遍好吗?除非你在contact.html.erb
中添加title
的内容,否则你的收益(:title)
不会有任何作用,我发布了一个更详细的答案,你也可以观看
module ApplicationHelper
def title(page_title = '')
content_for(:title) { page_title }
end
end
<%= title 'My Title' %>