RSpec规范失败了,我无法弄清楚

时间:2014-01-08 22:56:25

标签: ruby-on-rails ruby rspec

我讨厌成为一个帮助吸血鬼,但我是新人,而且我已经看了将近两天了。

我正在研究Michael Hartl撰写的Ruby on Rails教程。我正在进行清单4.4中的测试,我将在下面列出。我试图通过检查的特定测试,看看页面是否有一个带有文本的选择器,“Ruby on Rails Tutorial Sample App”。我可能会在问这个问题的过程中找到这个问题的答案,但是我们走了。以下是我认为的相关文件:

/spec/requests/static_pages_spec.rb

require 'spec_helper'

describe "StaticPages" do

  describe "Home page" do

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

    it "should have the base title" do
      visit '/static_pages/home'
      page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App")
    end

    it "should not have a custom page title" do
      visit '/static_pages/home'
      page.should_not have_selector('title', :text => '| Home')
    end

  end 
  ...[other tests for other pages, let me know if you think they're necessary]

end

应用/助手/ application_helper.rb

module ApplicationHelper
    # It is a good idea to put controller specific helpers in their 
    # related helper file (e.g., helpers specifically for the 
    #   StaticPages controller). 

    # Returns the full title on a per-page basis:
    #   This helper will be used for each page's title (unless
    #   otherwise noted).
    def full_title(page_title)
        base_title = "Ruby on Rails Tutorial Sample App"
        if page_title.empty?
            base_title
        else
            "#{base_title} | #{page_title}"
        end
    end

end

应用/视图/布局/ application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title><%= full_title(yield(:title))%></title>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

应用/视图/ static_pages / home.html.erb

<% provide :title, "Home" %>
<h1>Sample App</h1>
<p>
    This is the home page for a
    <a href="http://railstutorial.org/">Ruby on Rails</a> tutorial.
</p>
<p>
    It's by <a href="https://github.com/mhartl">Michael Hartl</a>
</p>

我还没有配置Sublime来在控制台中运行rspec。当我从bash在终端中运行测试时,我得到以下内容:

.......F.

Failures:

  1) StaticPages Home page should have the base title
     Failure/Error: page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App")
       expected #has_selector?("title", {:text=>"Ruby on Rails Tutorial Sample App"}) to return true, got false
     # ./spec/requests/static_pages_spec.rb:14:in `block (3 levels) in <top (required)>'

Finished in 0.14273 seconds
9 examples, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:12 # StaticPages Home page should have the base title

Randomized with seed #####

当我查看chrome中的源代码时,我可以清楚地看到一对带有“Ruby on Rails Tutorial Sample App | Home”的标签。

嗯,我已经完成了所有这些,我无法弄清楚发生了什么。你好,SO世界!请帮忙。谢谢。

我现在会向github推送,但他们已经失败了。我会在可能的情况下更新。

1 个答案:

答案 0 :(得分:1)

变化

have_selector('title', :text => "Ruby on Rails Tutorial Sample App")

have_title("Ruby on Rails Tutorial Sample App | Home")

这是对Capybara的更改,并在Michael Hartl的tutorial中进行了更新。要在教程中查看更新搜索“更改has_selector('title',...)到have_title(...)”一次。