Ruby on Rails教程第3章:为什么我的测试失败了?

时间:2018-01-26 16:31:51

标签: ruby-on-rails

如果有人能帮我弄清楚为什么我的测试测试失败,那将是一个很大的帮助!我以为我完全遵循每一步,但$ rails test命令导致失败!我已经回顾了我的所有步骤,并确保没有错别字。我目前在分支机构中运行$rails test命令,并使用Cloud 9,一个云开发环境。

静态页面控制器

    require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest
  def setup
    @base_title = "Ruby on Rails Tutorial Sample App"
  end

  test "should get home" do
    get static_pages_home_url
    puts response.body
    assert_response :success
    assert_select "title", "Home | #{@base_title}", response.body
  end

  test "should get help" do
    get static_pages_help_url
    puts response.body
    assert_response :success
    assert_select "title", "Help | #{@base_title}", response.body
  end

  test "should get about" do
    get static_pages_about_url
    puts response.body
    assert_response :success
    assert_select "title", "About | #{@base_title}", response.body
  end

end

About.html.erb

<% provide(:title, "About") %>
<h1>About</h1>
<p>
  The <a href="http://www.railstutorial.org/"><em>Ruby on Rails Tutorial</em></a> is a 
  <a href="http://www.railstutorial.org/book">book</a> and
  <a href="http://www.screencasts.railstutorial.org/"> screencast series</a> 
  to teach web development with 
  <a href="http://www.rubyonrails.org/">Ruby on Rails</a>.
  This is the sample application for the tutorial.
</p>

Home.html.erb

<% provide(:title, "Home") %>
<h1>Sample App</h1>
<p>
  This is the home page for the <a href="http://www.railstutorial.org/">Ruby on Rails Tutorial</a> sample application.
</p>

Application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title><%= yield(:title) %> | Ruby on Rails Tutorial Sample App</title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

我收到关于“关于”,“主页”和“帮助”测试的相同失败消息。我根据一些评论者的建议添加了response.body。

失败消息

F

Failure:
StaticPagesControllerTest#test_should_get_about [/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:26]:
<h1>About</h1>
<p>
  The <a href="http://www.railstutorial.org/"><em>Ruby on Rails
  Tutorial</em></a> is a
  <a href="http://www.railstutorial.org/book">book</a> and
  <a href="http://screencasts.railstutorial.org/">screencast series</a>
  to teach web development with
  <a href="http://rubyonrails.org/">Ruby on Rails</a>.
  This is the sample application for the tutorial.
</p>
.
Expected 0 to be >= 1.

/usr/local/rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/test_unit/reporter.rb:70:in `method': undefined method `test_should_get_about' for class `Minitest::Result' (NameError)
        from /usr/local/rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/test_unit/reporter.rb:70:in `format_rerun_snippet'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/test_unit/reporter.rb:23:in `record'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:786:in `block in record'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:785:in `each'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:785:in `record'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:334:in `run_one_method'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:321:in `block (2 levels) in run'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:320:in `each'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:320:in `block in run'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:360:in `on_signal'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:347:in `with_info_handler'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:319:in `run'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/test_unit/line_filtering.rb:9:in `run'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:159:in `block in __run'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:159:in `map'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:159:in `__run'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:136:in `run'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/minitest-5.11.1/lib/minitest.rb:63:in `block in autorun'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:171:in `fork'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:171:in `serve'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:141:in `block in run'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `loop'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `run'
        from /usr/local/rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application/boot.rb:19:in `<top (required)>'
        from /usr/local/rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /usr/local/rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from -e:1:in `<main>'

1 个答案:

答案 0 :(得分:0)

这看起来像布局错误。您的断言正在检查类似&#34;关于|的字符串网站名称&#34;,但您的输出不包含标题标记,它只显示正文,而不是application.html.erb中的任何内容。

您需要确保您的控制器使用application.html.erb中定义的布局呈现您的视图(默认情况下会发生这种情况,但可能是某些内容配置错误或您的控制器使用layout: false之类的内容进行渲染)。 / p>