【问题标题】:Can't test engine routes under Rails 3.2 + Rspec 2.13无法在 Rails 3.2 + Rspec 2.13 下测试引擎路线
【发布时间】:2013-06-26 05:46:52
【问题描述】:

在我的引擎“Utilizer”中,我正在尝试使用 Rspec 3.2 测试路线。 命名空间是孤立的

# lib/utilizer/engine.rb
module Utilizer
    class Engine < ::Rails::Engine
        isolate_namespace Utilizer
        ...
    end
end

引擎已安装到虚拟应用程序:

    # spec/dummy/config/routes.rb
    Rails.application.routes.draw do
      mount Utilizer::Engine => "/utilizer", as: 'utilizer'
    end

在 spec_helper.rb 中,我添加了如下几个配置(来自 here):

# spec/spec_helper.rb
RSpec.configure do |config|
  ...
  config.before(:each, type: :routing)    { @routes = Utilizer::Engine.routes }
  ...
end 

当我定义路线时:

# config/routes.rb
Utilizer::Engine.routes.draw do
  resources :auths, id: /\d+/, only: [:destroy]
end

Rake 为虚拟应用正确显示它:

$ spec/dummy > bundle exec rake routes

$ utilizer /utilizer Utilizer::Engine
$ Routes for Utilizer::Engine:
$ auth DELETE /auths/:id(.:format) utilizer/auths#destroy {:id=>/\d+/}

但是两个 Rspec 测试

# spec/routing/auths_routing_spec.rb
require 'spec_helper'

describe "Auths page routing" do

  let!(:auth) { create(:utilizer_auth) } # factory is working properly by itself

  describe "#destroy" do
    let!(:action) { { controller: "utilizer/auths", action: "destroy", id: auth.id } }
    specify { { delete: "/auths/#{ auth.id }" }.should route_to(action) }
    specify { { delete: auth_path(auth) }.should route_to(action) }
  end
end

因错误而失败(对应于第一个和第二个测试):

No route matches "/auths/1"
No route matches "/utilizer/auths/1"

但是,福尔摩斯,为什么?

【问题讨论】:

    标签: ruby-on-rails-3 rspec routing rspec-rails rule-engine


    【解决方案1】:

    我在 this discussion on Github 末尾的 Exoth 评论中找到了解决方案(感谢 Brandan)。

    在我的 spec_helper 中而不是

    config.before(:each, type: :routing) { @routes = Utilizer::Engine.routes }
    

    我用

    config.before(:each, type: :routing) do
      @routes = Utilizer::Engine.routes
      assertion_instance.instance_variable_set(:@routes, Utilizer::Engine.routes)
    end
    

    它有效。

    【讨论】:

      【解决方案2】:

      从 RSpec 2.14 开始,您可以使用以下内容:

      describe "Auths page routing" do
        routes { Utilizer::Engine.routes }
        # ...
      end
      

      来源:https://github.com/rspec/rspec-rails/pull/668

      【讨论】:

      • 为我工作。 config.before 在 rails 4.0.2 下不起作用。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多