RSpec失败:页面应该有标题

时间:2014-06-15 22:00:50

标签: ruby-on-rails ruby rspec

我正在尝试在Rails教程之后运行非常简单的RSpec测试,并且当它们不应该时它们会出乎意料地失败。

Fiona其他地方建议在app / views / layouts中移动文件application.html.erb,但我已经在那里了。

Zetetic建议添加“渲染视图”,但我做了,没有任何改变。

我正在使用的版本如下:

  • rvm 1.25.26

  • rspec 3.0.1

  • ruby​​ 1.9.3p547

  • Rails 4.1.1

我收到以下失败消息:

$>rspec spec/requests/pages_spec.rb
F

Failures:

1) Pages Home page should have the h1 'Sample App'
 Failure/Error: page.should have_selector('h1', :text => 'Sample App')
   expected #has_selector?("h1", {:text=>"Sample App"}) to return true, got false
 # ./spec/requests/pages_spec.rb:9:in `block (3 levels) in <top (required)>'

Deprecation Warnings:

Requiring `rspec/autorun` when running RSpec via the `rspec` command is deprecated. Called from /home/iammyr/.rvm/gems/ruby-1.9.3-p547/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'.

Using `should` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from /home/iammyr/railsgirls-app/projects/galway_june2014/spec/requests/pages_spec.rb:9:in `block (3 levels) in <top (required)>'.


Failed examples:

rspec ./spec/requests/pages_spec.rb:7 # Pages Home page should have the h1 'Sample App'

与本教程的一个不同之处在于,我没有使用“rails generate static_pages”生成那些静态页面,而是运行了“rails generate controller pages home help”,但这与rspec结果不一致,imho。

文件如下所示 的 pages_spec.rb

require 'spec_helper'

describe "Pages" do
  render_views
  describe "Home page" do
    it "should have the h1 'Sample App'" do
      visit '/pages/home'
      page.should have_selector('h1', :text => 'Sample App')
    end
  end
end

home.html.erb

<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
   This is the home page.
</p>

万分感谢任何想帮助我的人!谢谢! ;)

2 个答案:

答案 0 :(得分:1)

您应首先将规范从请求移至功能文件夹see changes in capybara

我想它会修复它(记得有一个类似的) 如果没有用pry调试它,在Firefox中用selenium驱动程序调试

 #Gemfile
  gem 'pry-rails'

  #spec
  it "should have the h1 'Sample App'", :js => true do
  visit '/pages/home'
  binding.pry
  page.should have_selector('h1', :text => 'Sample App')
end

Firefox应该启动并呈现页面 然后在调试模式下检查水豚选择器

   page.all(:css, 'h1')

答案 1 :(得分:0)

也许您没有定义capybara或其他驱动程序。然后,您尝试content而不是text

page.should have_selector('h1', :content => 'Sample App')