【问题标题】:Rails pages_controller_spec.rb test shouldn't be failing but is, error?Rails pages_controller_spec.rb 测试不应该失败,但是,错误?
【发布时间】:2012-01-18 20:29:09
【问题描述】:

一直在关注 Michael Hart 的 Rails 教程 mac OS X 10.7 上的 rails 版本 3.0

$ rspec 规范/

......FF

Failures:

  1) PagesController GET 'help' should be successful
     Failure/Error: get 'help'
     ActionController::RoutingError:
       No route matches {:controller=>"pages", :action=>"help"}
     # ./spec/controllers/pages_controller_spec.rb:45:in `block (3 levels) in <top (required)>'

  2) PagesController GET 'help' should have the right title
     Failure/Error: get 'help'
     ActionController::RoutingError:
       No route matches {:controller=>"pages", :action=>"help"}
     # ./spec/controllers/pages_controller_spec.rb:49:in `block (3 levels) in <top (required)>'

Finished in 0.14686 seconds
8 examples, 2 failures

Failed examples:

rspec ./spec/controllers/pages_controller_spec.rb:44 # PagesController GET 'help' should be successful
rspec ./spec/controllers/pages_controller_spec.rb:48 # PagesController GET 'help' should have the right title

测试看起来像这样:

require 'spec_helper'

describe PagesController do
  render_views

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

    it "should have the right title" do
      get 'home'
      response.should have_selector("title",
      :content => "Ruby on Rails Tutorial Sample App | Home")
    end
  end

  describe "GET 'contact'" do
    it "should be successful" do
      get 'contact'
      response.should be_success
    end
    it "should have the right title" do
      get 'contact'
      response.should have_selector("title",
      :content => "Ruby on Rails Tutorial Sample App | Contact")
    end
  end

  describe "GET 'about'" do
    it "should be successful" do
      get 'about'
      response.should be_success
    end
    it "should have the right title" do
      get 'about'
      response.should have_selector("title",
      :content => "Ruby on Rails Tutorial Sample App | About")
    end
  end

  describe "GET 'help'" do
    it "should be successful" do
      get 'help'
      response.should be_success
    end
    it "should have the right title" do
      get 'help'
      response.should have_selector("title",
      :content => "Ruby on Rails Tutorial Sample App | Help")
    end
  end
end

我在 pages_controller.rb 中有

class PagesController < ApplicationController
  def home
    @title = "Home"
  end

  def contact
    @title = "Contact"
  end

  def about
    @title = "About"
  end

  def help
    @title = "Help"
  end

end

在 routes.rb 我有

SampleApp::Application.routes.draw do
  get "pages/home"
  get "pages/contact"
  get "pages/about"
  get "pages/help"
end

当然,我还在 app/views/pages 中创建了一个 help.html.erb 页面 奇怪的是,当我运行 rails server 并转到 localhost:3000/pages/help 时,我得到了具有正确标题的正确页面,使它看起来好像测试应该通过了,但事实并非如此。此外,联系人、家庭和关于测试通过了,但是当我刚刚添加帮助时,由于某些未知原因,它并没有通过。这真的让我很烦,我忽略了它让我发疯的简单解决方案是什么?

【问题讨论】:

  • 出于某种原因,我的 help.html.erb 文件是 275 字节,而我的 about、contact 和 home .html.erb 页面都是 2kb,尽管它们的数量几乎完全相同每个中的字符。这肯定与 help.html.erb 在测试时不起作用有关。此外,寻求帮助的文档类型是 TextMate 文档,而其他文档类型只是说“文档”。奇怪..?
  • 我认为你在正确的轨道上——代码中没有任何东西会出现故障。我会尝试删除并重新创建 help.html.erb 文件。
  • 感谢您的想法,不幸的是它没有帮助。尤其奇怪的是,它给了我一个错误,即 rake 路线与此处解释的矛盾:stackoverflow.com/questions/8917201/my-computer-hates-me
  • 您的另一个问题被否决并不奇怪。不要使用讽刺性的标题来提问或诅咒——这是非常不受欢迎的。你为什么不把你的代码发布到 github 并链接到它——我会帮你看看,让你找到正确的方向。
  • 抱歉不合适,我已经推到这里了github.com/lasernite/sample_app

标签: ruby-on-rails testing rspec routes


【解决方案1】:

下载您的代码并运行:

........
8 examples, 0 failures
Finished in 0.18184 seconds

它正在运行 GET 'help',所以我认为你是在自动测试中运行它并且它没有重新加载。可能吗?

【讨论】:

  • 谢谢,退出终端并打开新窗口,一切正常。简单就是神奇。
【解决方案2】:

您的代码很好。问题是您之前的代码被缓存了。通过退出终端并打开一个新窗口,您可以有效地清除缓存。如果您不在测试环境中关闭缓存,您可能会遇到同样的问题。转到config/environments/test.rb 并将config.cache_classes = true 更改为config.cache_classes = false

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多