所以这是一个奇怪的问题:当我启动我的本地 Rails 应用程序并浏览到http://localhost:3000/static_pages/help时,我可以看到我在那里创建的页面。但是,我写的测试用例另有说明。
static_pages_controller_test.rb
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get :home
assert_response :success
end
test "should get help" do
puts static_pages_help_url
puts static_pages_help_path
get static_pages_help_url
assert_response :success
end
end
它失败并出现此错误,$bin/rake 测试的输出:
Running:
..http://test.host/static_pages/help
/static_pages/help
E
Finished in 0.466745s, 8.5700 runs/s, 4.2850 assertions/s.
1) Error.
StaticPagesControllerTest#test_should_get_help:
ActionController::UrlGenerationError: No route matches {:action=>"http://test.host/static_pages/help", :controller=>"static_pages"}
test/controllers/static_pages_controller_test.rb:12:in `block in <class:StaticPagesControllerTest>'
这是routes.rb
Rails.application.routes.draw do
get 'static_pages/home'
get "static_pages/help"
end
这是static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
end
和这两个文件
app/views/static_pages/home.html.erb
app/views/static_pages/help.html.erb
存在,因为我在浏览器中导航到 /static_pages/help 时也可以看到它们。我已经在网上搜索了几个小时,没有任何线索。
$ rails --version
Rails 4.2.7.1
$ ruby --version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
我肯定错过了什么。请帮忙。