当我开始在警卫中遇到此错误并且无法找到与任何方法有关的任何事情时,我很高兴地在 Hartl 的 tut 中工作(确切地在这里https://www.railstutorial.org/book/filling_in_the_layout#code-contact_page_test )或有关'[]'的方法调用,非常感谢任何帮助。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>
==================================================== ======== app/helpers/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
=====================