【问题标题】:RSpec spec fail and I can't figure it outRSpec 规范失败,我无法弄清楚
【发布时间】:2014-01-09 07:39:13
【问题描述】:

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

我正在编写 Michael Hartl 的 Ruby on Rails 教程。我在清单 4.4 中运行测试,我将在下面包含它。我试图通过的特定测试检查页面是否有一个带有文本“Ruby on Rails Tutorial Sample App”的选择器。在 SO 上提问的过程中,我可能会找到这个问题的答案,但我们开始吧。以下是我认为的相关文件:

/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

app/helpers/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

app/views/layouts/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>

app/views/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”的标签。

好吧,我已经经历了这一切,但我无法弄清楚发生了什么。你好,世界!请帮忙。谢谢你。

我现在会推送到 github,但它们已关闭。我会尽可能更新。

【问题讨论】:

标签: ruby-on-rails ruby rspec


【解决方案1】:

改变

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    相关资源
    最近更新 更多