ruby-on-rails - Ruby on Rails 教程第 5 章 Failure/Error it {should have_selector

标签 ruby-on-rails ruby testing rspec

我正在研究 Ruby on Rails 教程(第 5 章),但遇到错误。我的代码在....

https://github.com/Hjack/sample_app_new

我收到以下错误...

hakimus-MacBook-Air:sample_app_new hakimujackson$ bundle exec rspec spec/requests/static_pages_spec.rb
Rack::File headers parameter replaces cache_control after Rack 1.5.
...FFFFFF

Failures:

  1) Help page
     Failure/Error: it { should have_selector('h1',   text: 'Help')}
       expected css "h1" with text "Help" to return something
     # ./spec/requests/static_pages_spec.rb:20:in `block (2 levels) in <top (required)>'

  2) 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:21:in `block (2 levels) in <top (required)>'

  3) About page
     Failure/Error: it { should have_selector('h1',  text: 'About')}
       expected css "h1" with text "About" to return something
     # ./spec/requests/static_pages_spec.rb:28:in `block (2 levels) in <top (required)>'

  4) 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:29:in `block (2 levels) in <top (required)>'

  5) Contact page
     Failure/Error: it { should have_selector('h1',   text: 'Contact')}
       expected css "h1" with text "Contact" to return something
     # ./spec/requests/static_pages_spec.rb:36:in `block (2 levels) in <top (required)>'

  6) 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:37:in `block (2 levels) in <top (required)>'

Finished in 0.21916 seconds
9 examples, 6 failures

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:20 # Help page
rspec ./spec/requests/static_pages_spec.rb:21 # Help page
rspec ./spec/requests/static_pages_spec.rb:28 # About page
rspec ./spec/requests/static_pages_spec.rb:29 # About page
rspec ./spec/requests/static_pages_spec.rb:36 # Contact page
rspec ./spec/requests/static_pages_spec.rb:37 # Contact page

这是我的 spec.rb 文件...

require 'spec_helper'

describe "StaticPages" 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
  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

我不确定我做错了什么。如果有任何帮助,我将不胜感激。

谢谢!!!

最佳答案

问题出在你的规范文件中——

describe "StaticPages" 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
end

您过早地结束了 rspec 上下文——将最后一个“结束”移动到您的规范的底部,您的测试应该全部通过。

干杯!

关于ruby-on-rails - Ruby on Rails 教程第 5 章 Failure/Error it {should have_selector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17517022/

相关文章:

ruby-on-rails - Rails View 中的三元运算符

ruby - Textmate + Ruby : ruby: warning: -K is specified; it is for 1. 8 兼容性,可能导致奇怪的行为

ruby - 我如何修改此类以使用单例模式,如 activemodel?

python - 如何开始测试现有的 python 脚本?

testing - 如何在本地运行 archetypes.querywidget 测试套件?

ruby-on-rails - 什么是 rubocop 中的阴影外部局部变量,我该如何解决?

ruby-on-rails - Rails - 在提交按钮上覆盖模型名称

ruby-on-rails - ActiveModel::Dirty 和 JSON 字段

ruby - 如何使用 URL 在 Ruby 中克隆 git

c# - 编程招聘测试 - 测试开发人员在 C#/ASP.NET 中的知识