简体   繁体   English

为什么此Rails Rspec测试无法正常工作?

[英]Why doesn't this Rails Rspec Test Work?

I create all the routes of my program manually and so do with my rspec tests of course. 我手动创建程序的所有路由,当然也要对rspec测试进行创建。 Generally, my routes and tests work fine, but i have a problem with the test for my characters controller. 通常,我的路线和测试工作正常,但我的角色控制器的测试有问题。 The route is : 路线是:

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

The /characters works fine when tested with my browser. 使用我的浏览器进行测试时,/字符可以正常工作。 Everything seems fine. 一切似乎都很好。 But, the test : 但是,测试:

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

Fails with the error : 因错误而失败:

  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

All my controllers have similar tests that work fine. 我所有的控制器都有类似的测试,可以正常工作。 Why does this not work ? 为什么这不起作用?

IMPORTANT EDIT : 重要编辑:

Just saw that if i turn Spork off, the test passes ! 刚刚看到,如果我关闭Spork,则测试通过! Why is this happening ? 为什么会这样呢? Does Spork need to be restarted every time a new test is added ? 每次添加新测试时,都需要重新启动Spork吗?

You have to restart spork when changing routes. 更改路线时必须重新启动spork。

Or put this in your spec_helper.rb: 或将其放在您的spec_helper.rb中:

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

See also "Speedy Test Iterations for Rails 3 with Spork and Guard" 另请参阅“带有Spork和Guard的Rails 3的快速测试迭代”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM