簡體   English   中英

RSpec規范失敗,我無法弄清楚

[英]RSpec spec fail and I can't figure it out

我不想成為一名幫助吸血鬼,但是我是新來的,而且我已經看了將近兩天了。

我正在研究Michael Hartl編寫的Ruby on Rails教程。 我正在清單4.4中進行測試,下面將對此進行介紹。 我要通過的特定測試將檢查頁面是否具有文本為“ Ruby on Rails教程示例應用程序”的選擇器。 在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

應用程序/佣工/ 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教程示例應用程序|主頁”。

好吧,我經歷了所有事情,無法弄清楚發生了什么。 您好,世界! 請幫忙。 謝謝。

我現在會推送到github,但是它們已經失敗了。 可能的話我會更新。

更改

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

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

這是對Capybara的更改,並在Michael Hartl的教程中進行了更新。 要查看更新,請在本教程中一次搜索“將have_selector('title',…)更改為have_title(...)”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM