Ruby on Rails第3版 - 第3章练习3.6.2

时间:2014-12-22 03:46:04

标签: ruby-on-rails-4

我是ruby on rails的新手,并通过Ruby on rails第3版在线版学习它。在练习3.6.2中,它需要在sample_app中添加一个联系页面,Iam运行测试失败,我无法弄清楚。失败消息如下 -

   1) Failure:
    StaticPagesControllerTest#test_should_get_help [/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:13]:
  <Help | Ruby on Rails Tutorial Sample App> expected but was 
  <Help | Ruby on Rails tutorial Sample App>..
  Expected 0 to be >= 1.
   2) Failure:
   StaticPagesControllerTest#test_should_get_contact [/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:23]:
   <Contact | Ruby on Rails Tutorial Sample App> expected but was
   <Contact | Ruby on Rails tutorial Sample App>..
   Expected 0 to be >= 1.

route.rb文件如下 -

Rails.application.routes.draw do

root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
get 'static_pages/contact'

end

测试文件static_pages_controller.rb如下 -

require 'test_helper'

class StaticPagesControllerTest < ActionController::TestCase
   test "should get home" do
     get :home
     assert_response :success
     assert_select "title" , "Home | Ruby on Rails Tutorial Sample App"
 end

   test "should get help" do
      get :help
      assert_response :success
      assert_select "title" , "Help | Ruby on Rails Tutorial Sample App"
end
   test "should get about" do
      get :about
      assert_response :success
      assert_select "title" , "About | Ruby on Rails Tutorial Sample App"
end
   test "should get contact" do
      get :contact
      assert_response :success
      assert_select "title" , "Contact | Ruby on Rails Tutorial Sample App"
 end
end

应用程序文件application.html.erb是 -

<!DOCTYPE html>
     <html>
      <head>
     <title> <%= yield(:title) %> | Ruby on Rails tutorial Sample App</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>

我也是在分店里面做的 -

  

〜/ workspace / sample_app(static-pages-exercise)

而不是主人。

代码正在访问该页面,如果我通过浏览器我的云端识别它,它就可以工作。

Plz扔了一些光,谢谢。

1 个答案:

答案 0 :(得分:2)

当StackOverflow的代码突出显示器点亮您粘贴的错误消息中的语法时,它尤其引人注目;问题是你的标题有一个略有不同的情况:

测试正在寻找

Ruby on Rails **Tutorial** Sample App

但你拼写了#34;教程&#34;在<title>代码中使用小写字母T:

<title> <%= yield(:title) %> | Ruby on Rails **tutorial** Sample App</title>

(Asterisks mine)

tutorial更改为Tutorial,您的测试应该通过。