为什么这个Rails Rspec测试不起作用?

时间:2011-02-28 09:45:10

标签: ruby-on-rails rspec

我手动创建程序的所有路径,当然我的rspec测试也是如此。通常,我的路由和测试工作正常,但我的字符控制器测试有问题。路线是:

  scope :path => '/characters', :controller => :characters do
    get '/' => :show, :as => 'user_character'
  end  

使用浏览器测试时,/字符工作正常。一切似乎都很好。但是,测试:

require 'spec_helper'
require 'devise/test_helpers'

describe CharactersController do
    login_user

    describe "when it GETS 'show'" do
        it "should render the template and be successful" do
            get :show
            response.should render_template(:show)
            response.should be_success
        end
    end

end

因错误而失败:

  1) CharactersController when it GETS 'show' should render the template and be successful
     Failure/Error: get :show
     ActionController::RoutingError:
       No route matches {:controller=>"characters", :action=>"show"}
     # ./spec/controllers/characters_controller_spec.rb:9

我的所有控制器都有类似的测试,工作正常。为什么这不起作用?

重要编辑:

刚看到如果我关掉Spork,测试就过去了!为什么会这样?每次添加新测试时都需要重新启动Spork吗?

1 个答案:

答案 0 :(得分:3)

更改路线时必须重新启动spork。

或者将它放在spec_helper.rb中:

Spork.each_run do
  ApplicationName::Application.reload_routes!
end

See also "Speedy Test Iterations for Rails 3 with Spork and Guard"