2

RSpec 2 で Rails 3.1 エンジンをテストしようとしています。多くの試行錯誤 (およびドキュメントとスタック オーバーフローの検索) の後、アプリは動作し、ほとんどの仕様に合格しました。問題は、ルート仕様がまだ失敗していることです。

分離された名前空間とコントローラー Foo::BarsController を持つエンジン「foo」の場合、次のようになります。

require "spec_helper"

describe Foo::BarsController do
  describe "routing" do
    it "routes to #index" do
      get("/foo/bars").should route_to("bars#index")
    end

    it "routes to #new" do
      get("/foo/bars/new").should route_to("bars#new")
    end
  end
end

これにより、次の結果が得られます。

1) Foo::BarsController routing routes to #index
   Failure/Error: get("/foo/bars").should route_to("bars#index")
   ActionController::RoutingError:
     No route matches "/foo/bars"
   # ./spec/routing/foo/bars_routing_spec.rb:6:in `block (3 levels) in <top (required)>'

2) Foo::BarsController routing routes to #new
   Failure/Error: get("/foo/bars/new").should route_to("bars#new")
   ActionController::RoutingError:
     No route matches "/foo/bars/new"
   # ./spec/routing/foo/bars_routing_spec.rb:10:in `block (3 levels) in <top (required)>'

私のスペックダミーアプリケーションは正しく設定されているようです:

Rails.application.routes.draw do
  mount Foo::Engine => "/foo"
end

この質問に答えるのに役立つ場合、私のビュー仕様も機能していません。典型的なエラーは次のとおりです。

9) foo/bars/index.html.erb renders a list of bars
   Failure/Error: render
   ActionView::Template::Error:
     undefined local variable or method `new_bar_path' for #<#<Class:0x00000100c14958>:0x0000010464ceb8>
   # ./app/views/foo/bars/index.html.erb:3:in `___sers_matt__ites_foo_app_views_foo_bars_index_html_erb___1743631507081160226_2184232780'
   # ./spec/views/foo/bars/index.html.erb_spec.rb:12:in `block (2 levels) in <top (required)>'

何か案は?

4

4 に答える 4

1

get特定のルートを使用するように方法を変更してみてください

get("/foo/bars", :use_route => :foo)
于 2011-09-21T20:18:14.317 に答える
1

OK、これでうまくいきました!そして、それについてブログ記事を書きました。

http://www.matthewratzloff.com/blog/2011/09/21/testing-routes-with-rails-3-1-engines/

エンジン ルートをテスト用のアプリケーション ルート セットにインポートするには、新しいメソッドが必要です。

編集:名前付きルートの処理でバグを見つけて修正しました。

于 2011-09-21T22:09:37.143 に答える