【问题标题】:Hartl's Rails Tutorial Section 3.5Hartl 的 Rails 教程第 3.5 节
【发布时间】:2014-02-02 16:12:02
【问题描述】:

我一直在学习 Michael Hartl 的 Rails 教程,但由于某种原因,我卡在第 3 节的第一个练习中。我检查并重新检查了我的代码以确保它与他的匹配,但我仍然得到这个错误:

Failures:

  1) Static pages Contact page should have the content 'Contact'
     Failure/Error: expect(page).to have_content('Contact')
       expected #has_content?("Contact") to return true, got false
     # ./spec/requests/static_pages_spec.rb:48:in `block (3 levels) in <top (required)>'

  2) Static pages Contact page should have the title 'Contact'
     Failure/Error: expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
       expected #has_title?("Ruby on Rails Tutorial Sample App | Contact") to return true, got false
     # ./spec/requests/static_pages_spec.rb:53:in `block (3 levels) in <top (required)>'

Finished in 0.09624 seconds
8 examples, 2 failures

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:46 # Static pages Contact page should have the content 'Contact'
rspec ./spec/requests/static_pages_spec.rb:51 # Static pages Contact page should have the title 'Contact'

这是我的代码

static_pages_spec.rb

require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

    it "should have the content 'Sample App'" do
      visit '/static_pages/home'
      expect(page).to have_content('Sample App')
    end

    it "should have the title 'Home'" do
      visit '/static_pages/home'
      expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")
    end
  end

  describe "Help page" do

    it "should have the content 'Help'" do
      visit '/static_pages/help'
      expect(page).to have_content('Help')
    end

    it "should have the title 'Help'" do
      visit '/static_pages/help'
      expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
    end
  end

  describe "About page" do

    it "should have the content 'About Us'" do
      visit '/static_pages/about'
      expect(page).to have_content('About Us')
    end

    it "should have the title 'About Us'" do
      visit '/static_pages/about'
      expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
    end
  end

  describe "Contact page" do

    it "should have the content 'Contact'" do
      visit '/static_pages/about'
      expect(page).to have_content('Contact')
    end

    it "should have the title 'Contact'" do
      visit '/static_pages/about'
      expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
    end
  end

end

application.html.erb,保存在app/views/layouts下

<!DOCTYPE html>
<html>
<head>
  <title>Ruby on Rails Tutorial Sample App | <%= 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>

contact.html.erb

<% provide(:title, 'Contact') %>
<h1>Contact</h1>
<p>
  Contact Ruby on Rails Tutorial about the sample app at the
  <a href="http://railstutorial.org/contact">contact page</a>.
</p>

routes.rb

SampleApp::Application.routes.draw do
  get "static_pages/home"
  get "static_pages/help"
  get "static_pages/about"
  get "static_pages/contact"
end

static_pages_controller.rb

class StaticPagesController < ApplicationController
  def home
  end

  def help
  end

  def about
  end

  def contact
  end
end

【问题讨论】:

  • contact.html.erb 在您的文件结构中位于何处?

标签: ruby-on-rails ruby rspec


【解决方案1】:

由于您提供的信息有限(我知道当您是初学者时很难诊断,但它会变得更容易)。您似乎在告诉您的测试检查您的关于页面并检查仅存在于您的联系页面中的内容。

【讨论】:

  • 啊,明白了。非常感谢!
猜你喜欢
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多