鲁比·纽比来了。我正在学习rails教程,在5.3re:routes上遇到了问题。
我有5页(主页,关于,帮助,联系人),都有类似的测试集,rspec只会在“主页”上测试失败因为我使用的是application_helper,所以不需要在home.html.erb中指定,对吧?我还接受了understanding rails routes: match vs root in routes.rb的建议,并将“match'/static_pages/home'=>“static_pages#home”添加到routes.db。
在这两个错误上纠结了一段时间请帮忙谢谢!
错误:

1) Static pages Home page should have the h1 'Sample App'
Failure/Error: page.should have_selector('h1', text: 'Sample App')
expected css "h1" with text "Sample App" to return something
# ./spec/requests/static_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

2) Static pages Home page should have the base title
Failure/Error: page.should have_selector('title',
expected css "title" with text "Ruby on Rails Tutorial Sample App" to return something
# ./spec/requests/static_pages_spec.rb:13:in `block (3 levels) in <top (required)>'

主页.html.erb
<div class="center hero-unit">
<h1>Welcome to the Sample App</h1>

<h2>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</h2>

<%= link_to "Sign up now!", '#', class: "btn btn-large btn-primary" %>
</div>

<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>

静态页面规范rb
require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

  it "should have the h1 'Sample App'" do
    visit root_path
    page.should have_selector('h1', text: 'Sample App')
  end

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

  it "should not have a custom page title" do
    visit root_path
    page.should_not have_selector('title', text: '| Home')
  end
end

应用程序.html.erb
<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= stylesheet_link_tag    "application", media: "all" %>
    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>
    <%= render 'layouts/shim' %>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <%= yield %>
      <%= render 'layouts/footer' %>
    </div>
  </body>
</html>

应用程序助手.rb
module ApplicationHelper

# Returns the full title on a per-page basis.
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

路线.rb
SampleApp::Application.routes.draw do
  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'
  match '/static_pages/home' => 'static_pages#home'

  root to: 'static_pages#home'
end

如果你还需要什么,请告诉我。谢谢!

最佳答案

您是否删除了public/index.html文件并通过视觉验证您是否可以访问/在浏览器中访问,以及预期的模板是否正在呈现?否则你的规格看起来没问题。

关于ruby - Rails教程-5.2节路由/Rspec错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10669951/

10-17 02:51