最终编辑:
感谢@PeterAlfvin 建议我专注于应用程序布局和标题,我能够修复它。我无法弄清楚到底是什么问题,但标题中的某些内容导致水豚看不到页面的其余部分。我删除<%= render 'layouts/header' %>
了,在里面重新构建了标题application.html.erb
,一旦它工作了,就把它拉出来变成一个部分并替换了渲染语句。我摆弄了新旧头文件以试图找出问题所在,但还没有成功。这让我的测试看起来有点脆弱,但如果问题再次出现,至少我会知道去哪里找。
编辑:如果您倾向于投反对票,请继续,但请告诉我我忽略或未能研究的内容,因为您的评论可能会帮助我解决这个问题。
我已经搜索了 HOURS 试图解决这个问题,但我无法弄清楚发生了什么。我敢肯定我错过了一些简单的东西,这对任何最后一天没有盯着它的人来说都是显而易见的。无论如何,这是我的希望。
我所有的 Rspec/capybara 请求规范都失败了,但是当我在浏览器中查看页面时,规范找不到的所有元素都在那里。据我所知,Capybara 没有看到整个页面 - save_and_open_page 返回一个几乎空白的页面。我在下面的规范中看到的失败是普遍的——规范可以看到页面标题,但在页面正文中看不到任何内容。我相当有信心我所有的路线都很好,因为所有的标题测试都在他们应该通过的时候通过。
规范/请求/static_pages_spec.rb
require 'spec_helper'
describe "StaticPages" do
subject { page }
describe "About page" do
before { visit about_path }
it { save_and_open_page;
should have_selector('title', text: 'Company Name | About') }
it { should have_selector('h1', text: 'About stuff') }
it { should have_content('About paragraph') }
end
#more specs...
end
结果:
5) StaticPages About page
Failure/Error: it { should have_selector('h1', text: 'About stuff') }
expected css "h1" with text "About stuff" to return something
# ./spec/requests/static_pages_spec.rb:28:in `block (3 levels) in <top (required)>'
6) StaticPages About page
Failure/Error: it { should have_content('About paragraph') }
expected there to be content "About paragraph" in "Company Name | About\n\t\n\t\t"
# ./spec/requests/static_pages_spec.rb:29:in `block (3 levels) in <top (required)>'
这是 save_and_open_page 在我的浏览器中打开的页面的“查看源代码”输出:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Company Name | About</title>
<link href="/assets/application.css" media="all" rel="stylesheet" type="text/css">
<script src="/assets/application.js" type="text/javascript"></script><!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<header class="navbar navbar-fixed-top navbar-inverse"><div class="navbar-inner">
</div></header>
</body>
</html>
因此,根据来源和规范 #6 的响应消息,它看起来除了页面标题之外没有搜索任何内容,这将得到以下事实的支持:我的标题测试始终通过,但没有其他任何内容。我有寻找表单元素、按钮等的测试,并且我尝试过以不同的方式识别它们(标签、id、名称),但水豚找不到它们中的任何一个。同样,当我在浏览器中查看这些页面时,一切都清晰可见,并且标记正确。
当我在浏览器中访问 About 页面时,这是 body 的页面源,与上面显示的 body 内容形成对比:
<body>
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<a class="brand" href='/'>Company Name</a>
<ul class="nav pull-left">
<li><a href="/products">Products</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
<ul class="nav pull-right">
<li><a href="/users/sign_in">Sign in</a></li>
</ul>
</div>
</header>
<div class="container-fluid">
<div class="row-fluid">
<div class="span10 offset2"></div>
<h1>About stuff</h1>
<p>About paragraph</p>
</div>
</div>
</body>
宝石文件:
source 'https://rubygems.org'
gem 'rails', '3.2.13'
gem 'bootstrap-sass', '2.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
gem 'jquery-rails'
gem 'devise'
gem 'foreigner'
gem 'paper_trail'
group :development, :test do
gem 'sqlite3', '1.3.7'
gem 'rspec-rails', '2.11.0'
gem 'letter_opener'
gem 'factory_girl_rails', '4.1.0'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.5'
gem 'coffee-rails', '3.2.2'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '1.2.3'
end
group :test do
gem 'capybara', '1.1.2'
# gem 'rb-inotify', '0.8.8'
# gem 'libnotify, '0.5.9'
gem 'shoulda-matchers'
gem 'accept_values_for'
end
group :production do
gem 'pg', '0.12.2'
end
任何有助于解决这个问题的帮助将不胜感激。如果我应该包含任何其他信息,请告诉我。
编辑以添加 erb 文件
应用程序/视图/布局/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 %>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<%= render 'layouts/header' %>
<div class="container-fluid">
<div class="row-fluid">
<div class="span10 offset2">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value%></div>
<% end %>
</div>
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</div>
</body>
</html>
应用程序/视图/布局/_header.html.erb
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<a class="brand" href='/'>Company Name</a>
<ul class="nav pull-left">
<li><%= link_to "Products", products_path %></li>
<li><%= link_to "About", about_path %></li>
<li><%= link_to "Contact", contact_path %></li>
</ul>
<% if user_signed_in? %>
<ul class="nav pull-right">
<li><strong><%= current_user.email %></strong></li>
<li><%= link_to 'Account', account_path %></li>
<li><%= link_to "Sign out", destroy_user_session_path, method: :delete %></li>
</ul>
<% else %>
<ul class="nav pull-right">
<li><%= link_to "Sign in", new_user_session_path %></li>
</ul>
<% end %>
</div>
</header>
应用程序/视图/布局/_footer.html.erb
<footer class="footer">
<small>Company Name Price Management</small>
</footer>
app/views/static_pages/about.html.erb
<% provide(:title, 'About') %>
<h1>About stuff</h1>
<p>About paragraph</p>