我在使用guard的rails 4上运行测试时遇到了未定义的方法错误

时间:2015-04-23 02:31:55

标签: ruby-on-rails minitest guard rake-test

我很高兴在Hartl的啧啧(正好在这里https://www.railstutorial.org/book/filling_in_the_layout#code-contact_page_test)工作,当我开始得到这个错误的警卫,并且无法找到任何关于&的任何方法或方法调用的任何事情#39; []',任何帮助非常感谢。 https://github.com/mgmacri/sample-app

02:23:12 - INFO - Running:
/home/ubuntu/workspace/sample_app/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /home/ubuntu/workspace/sample_app/config/application.rb to limit the frameworks that will be loaded.
Started

ERROR["test_should_get_contact", StaticPagesControllerTest, 5.435833117]
 test_should_get_contact#StaticPagesControllerTest (5.44s)
ActionView::Template::Error:         ActionView::Template::Error: undefined method `[]' for nil:NilClass
            app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb___423443063036763089_70222525057280'
            test/controllers/static_pages_controller_test.rb:24:in `block in <class:StaticPagesControllerTest>'
        app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb___423443063036763089_70222525057280'
        test/controllers/static_pages_controller_test.rb:24:in `block in <class:StaticPagesControllerTest>'

ERROR["test_should_get_help", StaticPagesControllerTest, 10.237566481]
 test_should_get_help#StaticPagesControllerTest (10.24s)
ActionView::Template::Error:         ActionView::Template::Error: undefined method `[]' for nil:NilClass
            app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb___423443063036763089_70222525057280'
            test/controllers/static_pages_controller_test.rb:12:in `block in <class:StaticPagesControllerTest>'
        app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb___423443063036763089_70222525057280'
        test/controllers/static_pages_controller_test.rb:12:in `block in <class:StaticPagesControllerTest>'

ERROR["test_should_get_about", StaticPagesControllerTest, 14.55194709]
 test_should_get_about#StaticPagesControllerTest (14.55s)
ActionView::Template::Error:         ActionView::Template::Error: undefined method `[]' for nil:NilClass
            app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb___423443063036763089_70222525057280'
            test/controllers/static_pages_controller_test.rb:18:in `block in <class:StaticPagesControllerTest>'
        app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb___423443063036763089_70222525057280'
        test/controllers/static_pages_controller_test.rb:18:in `block in <class:StaticPagesControllerTest>'

ERROR["test_should_get_home", StaticPagesControllerTest, 19.535717523]
 test_should_get_home#StaticPagesControllerTest (19.54s)
ActionView::Template::Error:         ActionView::Template::Error: undefined method `[]' for nil:NilClass
            app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb___423443063036763089_70222525057280'
            test/controllers/static_pages_controller_test.rb:6:in `block in <class:StaticPagesControllerTest>'
        app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb___423443063036763089_70222525057280'
        test/controllers/static_pages_controller_test.rb:6:in `block in <class:StaticPagesControllerTest>'

  4/4: [===================================] 100% Time: 00:00:19, Time: 00:00:19

Finished in 19.54385s
4 tests, 0 assertions, 0 failures, 4 errors, 0 skips

==================================================================

应用程序/视图/布局/ application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= stylesheet_link_tag "application", media: "all",
                                           "data-turbolinks-track" =>   true %>
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
    <%= csrf_meta_tags %>
    <%= render 'layouts/shim' %>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <%= yield %>
      <%= render 'layouts/footer' %>
    </div>
  </body>
</html>

=============================================== =========== 应用程序/助手/ application_helper.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
      "#{page_title} | #{base_title}"
    end
  end
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", "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

====================

https://github.com/mgmacri/sample-app

1 个答案:

答案 0 :(得分:0)

这本书错过了一条线。 在这部分Gemfile上添加gem'guard' group :development do gem 'guard' # NOTE: this is necessary in newer versions gem 'guard-minitest' end

来源:here