簡體   English   中英

Michael Hartl的Rails教程第3章,rspec出現錯誤

[英]Michael Hartl's rails tutorial chapter 3, Error in rspec when

我是Ruby的新手。 當前使用特立獨行的10.9.3,Rails4。嘗試運行rspec命令時出現以下錯誤。

這是我得到的錯誤:

rspec ./spec/controllers/pages_controller_spec.rb:35 # PagesController GET 'about' should have the right title
rspec ./spec/controllers/pages_controller_spec.rb:24 # PagesController GET 'contact' should have the right title
rspec ./spec/controllers/pages_controller_spec.rb:11 # PagesController GET 'home' should have the right title

我的Gemfile包括:

group :development, :test do
  gem 'rspec-rails', '2.14.2'
end

group :test do
  gem 'rspec', '2.14.1'
  gem 'spork', '0.9.0.rc'
end

還有我的pages_controller_spec.rb

require 'spec_helper'

describe PagesController do
  render_views

  describe "GET 'home'" do
    it "returns http success" do
      get 'home'
      response.should be_success
    end
    it "should have the right title" do
      get 'home'
      response.should have_selector("title", :content => "Ruby on Rails Tutorial Sample App | Home")
    end
  end

  describe "GET 'contact'" do
    it "returns http success" do
      get 'contact'
      response.should be_success
    end


    it "should have the right title" do
      get 'contact'
      response.should have_selector("title", :content => "Ruby on Rails Tutorial Sample App | Contact")
    end
  end
  describe "GET 'about'" do
    it "returns http success" do
      get 'about'
      response.should be_success
    end

    it "should have the right title" do
      get 'about'
      response.should have_selector("title", :content => "Ruby on Rails Tutorial Sample App | About")
    end
  end
end

控制器代碼來自以下注釋:

class PagesController < ApplicationController
  def home
  end

  def contact
  end

  def about
  end
end

您是否嘗試過檢查文檔: https : //relishapp.com/rspec

它與About和About的小寫字母“ a”一樣簡單嗎?

您的測試正在尋找字符串“ Ruby on Rails教程示例應用程序|關於”,而代碼正在生成“ Ruby on Rails教程示例應用程序|關於”。 據我所知,rspec是區分大小寫的。

我看到的另一件事是必須將:visible設置為false以測試標題。

您可能還會遇到這個錯誤Ruby on Rail Michael Hartl的Chapter 3 Error 您可能會因為內置的Rails測試套件(在撰寫本文時為'rails-controller-testing'-'1.0.2','minitest'-'5.10.3', 'minitest-reporters'-'1.1.14')

我建議您在閱讀Michael Hartl的書以學習Rails時遵循其Gemfiles。

provide將標記塊存儲在標識符中,以備后用。 在這種情況下,請在符號:title中使用“幫助”。 提供包含在<% %>以指示它正在執行此代碼且未在視圖中打印出來。

在這種情況下, yield只是吐出而被阻止。 產量包含在<%= %>以指示它已被打印到視圖中。

將其視為設置變量並打印出變量。

有關更多信息,請參見: http : //api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-provide 請注意,provide實際上是content_for的包裝,因此,該鏈接中的好地方就在這里。

這摘自以下StackOverflow文章: 模板中的yield和 Provide ()

暫無
暫無

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

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