Rails pages_controller_spec.rb test shouldn't be failing but is, error?

二次信任 提交于 2019-12-13 05:36:07

问题


Have been following Rails Tutorial by Michael Hart rails version 3.0 on mac OS X 10.7

$ rspec spec/

......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

The test looks like this:

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

And I have in 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

And in routes.rb I have

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

And of course I also created a help.html.erb page in app/views/pages The strange thing is when I run rails server and go to localhost:3000/pages/help I get the proper page with the proper title, making it appear as if the test should've passed, yet it doesn't. Additionally the contact, home, and about tests pass but when I just now added the help it doesn't for some unknown reason. This is really bugging me out, what is the simple solution I have overlooked that it driving me insane?


回答1:


Downloaded your code and ran:

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

It is running the GET 'help', so I think you're running this in autotest and it is not reloading. Possible?




回答2:


Your code is fine. The issue is that your previous code got cached. By quitting terminal and opening a new window, you are effectively clearing your cache. You might run into the same issues if you don't turn off caching in the test environment. Go to config/environments/test.rb and change config.cache_classes = true to config.cache_classes = false



来源:https://stackoverflow.com/questions/8917201/contradiction-between-rspec-and-rake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!