ruby-on-rails - Hartl 的教程 - 第 5.3 章布局链接

标签 ruby-on-rails ruby rspec routes railstutorial.org

好的...有点犹豫要不要发布这个问题,但这里是:

其实很像这样question .

我的规范测试也失败了......但我不太关心 b/c 它只涉及所有页面的标题......(我可以轻松修复)

我真正想知道的是路线问题。

在遵循 hartl 在 section 5.3.2 rail routes 中的所有指示之后,这就是我得到的:

No route matches [GET] "/static_pages/about"
No route matches [GET] "/static_pages/home"
No route matches [GET] "/static_pages/help"
No route matches [GET] "/static_pages/contact"

config/routes.rb 设置

SampleApp::Application.routes.draw do
   root to: 'static_pages#home'
   match '/help',    to: 'static_pages#help'
   match '/about',   to: 'static_pages#about'
   match '/contact', to: 'static_pages#contact' 

我可以通过在“/help”、“/about”和“/contact”前加上“static_pages”来修复上面的三个页面 ...

还是没有解决首页问题。

将此添加到 spec_helper.rb 没有帮助(来自链接)

config.include Rails.application.routes.url_helpers

我在这里遗漏了什么,我还需要添加哪些其他信息才能使问题更清楚?

测试通过以下语句完成:bundle exec rspec spec/requests/static_pages_spec.rb

这是运行语句后的终端输出

Failures:

1) Static pages About page should have the h1 'About Us'
 Failure/Error: visit '/static_pages/about'
 ActionController::RoutingError:
   No route matches [GET] "/static_pages/about"
 # ./spec/requests/static_pages_spec.rb:46:in `block (3 levels) in <top (required)>'

 2) Static pages About page should not have a custom page title
 Failure/Error: visit '/static_pages/about'
 ActionController::RoutingError:
   No route matches [GET] "/static_pages/about"
 # ./spec/requests/static_pages_spec.rb:57:in `block (3 levels) in <top (required)>'

 3) Static pages About page should have the base title
 Failure/Error: visit '/static_pages/about'
 ActionController::RoutingError:
   No route matches [GET] "/static_pages/about"
 # ./spec/requests/static_pages_spec.rb:51:in `block (3 levels) in <top (required)>'

 4) Static pages Home page should have the h1 'Sample App'
 Failure/Error: visit '/static_pages/home'
 ActionController::RoutingError:
   No route matches [GET] "/static_pages/home"
 # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

 5) Static pages Home page should not have a custom page title
 Failure/Error: visit '/static_pages/home'
 ActionController::RoutingError:
   No route matches [GET] "/static_pages/home"
 # ./spec/requests/static_pages_spec.rb:19:in `block (3 levels) in <top (required)>'

 6) Static pages Home page should have the base title
 Failure/Error: visit '/static_pages/home'
 ActionController::RoutingError:
   No route matches [GET] "/static_pages/home"
 # ./spec/requests/static_pages_spec.rb:13:in `block (3 levels) in <top (required)>'

 7) Static pages Help page should have the h1 'Help'
 Failure/Error: visit '/static_pages/help'
 ActionController::RoutingError:
   No route matches [GET] "/static_pages/help"
 # ./spec/requests/static_pages_spec.rb:27:in `block (3 levels) in <top (required)>'

 8) Static pages Help page should not have a custom page title
 Failure/Error: visit '/static_pages/help'
 ActionController::RoutingError:
   No route matches [GET] "/static_pages/help"
 # ./spec/requests/static_pages_spec.rb:38:in `block (3 levels) in <top (required)>'

 9) Static pages Help page should have the base title
 Failure/Error: visit '/static_pages/help'
 ActionController::RoutingError:
   No route matches [GET] "/static_pages/help"
 # ./spec/requests/static_pages_spec.rb:32:in `block (3 levels) in <top (required)>'

 10) Static pages Contact page should have the h1 'Contact'
 Failure/Error: visit '/static_pages/contact'
 ActionController::RoutingError:
   No route matches [GET] "/static_pages/contact"
 # ./spec/requests/static_pages_spec.rb:65:in `block (3 levels) in <top (required)>'

 11) Static pages Contact page should have the title 'Contact'
 Failure/Error: visit '/static_pages/contact'
 ActionController::RoutingError:
   No route matches [GET] "/static_pages/contact"
 # ./spec/requests/static_pages_spec.rb:70:in `block (3 levels) in <top (required)>'

 Finished in 0.14305 seconds
 11 examples, 11 failures

 Failed examples:

 rspec ./spec/requests/static_pages_spec.rb:45 # Static pages About page should have the h1 'About Us'
 rspec ./spec/requests/static_pages_spec.rb:56 # Static pages About page should not have a custom page title
 rspec ./spec/requests/static_pages_spec.rb:50 # Static pages About page should have the base title
 rspec ./spec/requests/static_pages_spec.rb:7 # Static pages Home page should have the h1 'Sample App'
 rspec ./spec/requests/static_pages_spec.rb:18 # Static pages Home page should not have a custom page title
 rspec ./spec/requests/static_pages_spec.rb:12 # Static pages Home page should have the base title
 rspec ./spec/requests/static_pages_spec.rb:26 # Static pages Help page should have the h1 'Help'
 rspec ./spec/requests/static_pages_spec.rb:37 # Static pages Help page should not have a custom page title
 rspec ./spec/requests/static_pages_spec.rb:31 # Static pages Help page should have the base title
 rspec ./spec/requests/static_pages_spec.rb:64 # Static pages Contact page should have the h1 'Contact'
  rspec ./spec/requests/static_pages_spec.rb:69 # Static pages Contact page should have the title 'Contact'

最佳答案

虽然您有一个根映射,它将“/”请求路由到 StaticPagesController home 操作,但是您没有路由 static_pages/home 的映射。添加它:

   match 'static_pages/home',    to: 'static_pages#home'

关于ruby-on-rails - Hartl 的教程 - 第 5.3 章布局链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12635369/

相关文章:

ruby-on-rails - 使用restful_authentication验证电子邮件帐户后rails自动登录

ruby-on-rails - 如何检查是否在网站上找到了图像?

ruby-on-rails - Ruby on Rails 中的关注点是否可以替代 MVC 中的服务类?

ruby-on-rails - 如何格式化此 PostgreSQL 查询以便能够与 db :migrate 一起运行

ruby-on-rails - 我怎样才能让自动测试在失败后继续运行我的规范而不停止?

ruby-on-rails - 如何为登录用户和非登录用户设置主页?

ruby - 如何在 Ruby 1.9 中读取文件中的字节?

ruby - Rspec let() 清理

ruby - 在 rspec 中描述。什么时候使用引号?

ruby-on-rails - 具有多个条件的 Rails 搜索