9

アプリケーションが使用しているエンジン内にあるコントローラーをテストしようとしています。仕様はエンジン内ではなく、アプリケーション自体にあります (エンジン内でテストしようとしましたが、問題もありました)。

私のエンジンには次のroutes.rbがあります:

Revision::Engine.routes.draw do
  resources :steps, only: [] do
    collection { get :first }
  end
end

エンジンは通常、アプリケーション routes.rb にマウントされます。

mount Revision::Engine => "revision"

を実行するrake routesと、最後の行で次のようになります。

Routes for Revision::Engine:
first_steps GET /steps/first(.:format) revision/steps#first
       root     /                       revision/steps#first

エンジンのコントローラー ( lib/revision/app/controllers/revision/steps_controller.rb) には、次のものがあります。

module Revision
  class StepsController < ApplicationController
    def first
    end
  end
end

Rspec では、このコントローラーを次のようにテストします。

require 'spec_helper'
describe Revision::StepsController do
  it "should work" do
    get :first
    response.should be_success
  end
end

次に、この仕様を実行すると、次のようになります。

ActionController::RoutingError:
   No route matches {:controller=>"revision/steps", :action=>"first"}

ルートが実際には存在しないことを確認するために、これを仕様に追加しました。

before do
  puts @routes.set.to_a.map(&:defaults)
end

結果は次のとおりです。

[...]
{:action=>"show", :controller=>"devise/unlocks"}
{:action=>"revision"}

パラメーターのみがあり:actionます。

何が間違っている可能性がありますか?

4

1 に答える 1

4

エンジンのコントローラーをテストしようとしている場合、コントローラー テストで使用するルート セットを指定する必要があります。そうしないと、メイン アプリに対して実行されます。use_route: :engine_nameこれを行うには、getメソッドに渡します。

require 'spec_helper'
describe Revision::StepsController do
  it "should work" do
    get :first, use_route: :revision # <- this is how you do it
    response.should be_success
  end
end
于 2013-08-09T22:17:14.303 に答える