【问题标题】:Rspec reliability with routing. (Rails by Example RoR Tutorial )Rspec 可靠性与路由。 (Rails 示例 RoR 教程)
【发布时间】:2011-12-27 08:44:06
【问题描述】:

在学习 Ruby on Rails 5.2 上的示例教程时,我对 Rspec 感到沮丧。

我目前正在尝试使用 rspec 测试我的路由,即使我尽我所知正确地遵循了教程中的步骤,并且通过手动检查和验证了路由是否有效,测试总是以失败的形式返回自己测试路由。

例如,layout_links_spec.rb 如下所示:

require 'spec_helper'

describe "LayoutLinks" do

    it "should have a Home page at '/'" do
        get '/'
        response.should have_selector('title', :content => "Home")
    end

    it "should have a Contact page at '/contact'" do
        get '/contact'
        response.should have_selector('title', :content => "Contact")
    end

    it "should have an About page at '/about'" do
        get '/about'
        response.should have_selector('title', :content => "About")
    end

    it "should have a Help Page at '/help'" do
        get '/help'
        reponse.should have_selector('title', :content => "Help")
    end

end

我的路线看起来像:

SampleApp::Application.routes.draw do
  match '/contact', :to => 'pages#contact'
  match '/about', :to => 'pages#about'
  match '/help', :to => 'pages#help'
  get "pages/home"
  get "pages/contact"
  get "pages/about"
  get "pages/help"

  root :to => 'pages#home'
end

Rspec 返回此失败:

  5) LayoutLinks should have a Home page at '/'
     Failure/Error: get '/'
     ActionController::RoutingError:
       No route matches [GET] "/"
     # ./spec/requests/layout_links_spec.rb:6:in `block (2 levels) in <top (required)>'

  6) LayoutLinks should have a Contact page at '/contact'
     Failure/Error: get '/contact'
     ActionController::RoutingError:
       No route matches [GET] "/contact"
     # ./spec/requests/layout_links_spec.rb:11:in `block (2 levels) in <top (required)>'

  7) LayoutLinks should have an About page at '/about'
     Failure/Error: get '/about'
     ActionController::RoutingError:
       No route matches [GET] "/about"
     # ./spec/requests/layout_links_spec.rb:16:in `block (2 levels) in <top (required)>'

  8) LayoutLinks should have a Help Page at '/help'
     Failure/Error: get '/help'
     ActionController::RoutingError:
       No route matches [GET] "/help"
     # ./spec/requests/layout_links_spec.rb:21:in `block (2 levels) in <top (required)>'

我的 pages_controller_spec.rb 测试也出现类似错误

  describe "GET 'home'" do
    it "should be successful" do
      get 'home'
      response.should be_success
    end

得到这个 rspec 错误:

  1) PagesController GET 'home' should be successful
     Failure/Error: get 'home'
     ActionView::Template::Error:
       undefined local variable or method `root_path' for #<#<Class:0x00000103e8b210>:0x00000103e87de0>
     # ./app/views/layouts/_header.html.erb:3:in `_app_views_layouts__header_html_erb__1560745193027372362_2179584160'
     # ./app/views/layouts/application.html.erb:10:in `_app_views_layouts_application_html_erb__2627526215404316040_2176993080'
     # ./spec/controllers/pages_controller_spec.rb:15:in `block (3 levels) in <top (required)>'

我用教程检查了我的代码,它看起来正确。如果我输入 url localhost:3000/home 或 /about 等,我会进入正确的页面,表明它必须正确路由。我可以继续学习本教程,但我想学习如何进行 TDD 并在实践中使用 rspec,但我觉得我不能依靠 rspec 来做到这一点......

【问题讨论】:

  • 如果我没记错的话,Rails 教程将 Spork 与 RSpec 结合使用 - 您是否在编辑路由文件后重新启动了 Spork 服务器? Spork 会预加载路由,需要重新启动才能使更改生效。
  • 迟到的回复迟到了,但是……谢谢!这解决了问题!在真正尝试掌握 BDD/TDD 和 Rspec 几个月后,我开始喜欢它。其实挺好玩的!
  • 你应该看看 Guard - 当关键文件改变时它会自动重启 spork 服务器。

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


【解决方案1】:

我遇到了完全相同的问题,并且差点扔东西。

重新启动 Spork 解决了这个问题,世界再次一切顺利

【讨论】:

    猜你喜欢
    • 2011-01-21
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多