3

私のエンジン「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 に、以下のように構成をいくつか追加しました (ここから):

# 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

エラーで失敗します (1 番目と 2 番目のテストに対応):

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

でも、ホームズ、どうして?

4

2 に答える 2

8

RSpec 2.14 以降、以下を使用できます。

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

ソース: https://github.com/rspec/rspec-rails/pull/668

于 2013-07-04T15:37:36.130 に答える
0

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

そしてそれは動作します。

于 2013-06-30T07:05:27.207 に答える