2

アプリをビルドするときに、標準の Rspec テストを作成する scaffold を生成しました。カバレッジのためにこれらのテストを使用したいのですが、ルートがネストされているために失敗しているようです。

テストを実行したときのフィードバックは次のとおりです。

Failures:

  1) ListItemsController routing routes to #index
     Failure/Error: get("/list_items").should route_to("list_items#index")
       No route matches "/list_items"
     # ./spec/routing/list_items_routing_spec.rb:7:in `block (3 levels) in <top (required)>'

Finished in 0.25616 seconds
1 example, 1 failure

ネストされたルートがあることをRspecに伝えるにはどうすればよいですか?

要約されたファイルは次のとおりです。

list_items_routing_spec.rb:

require "spec_helper"

describe ListItemsController do
  describe "routing" do

    it "routes to #index" do
      get("/list_items").should route_to("list_items#index")
    end

end

list_items_controller_spec.rb:

describe ListItemsController do
  # This should return the minimal set of attributes required to create a valid
  # ListItem. As you add validations to ListItem, be sure to
  # adjust the attributes here as well.
  let(:valid_attributes) { { "list_id" => "1", "project_id" => "1"  } }

  # This should return the minimal set of values that should be in the session
  # in order to pass any filters (e.g. authentication) defined in
  # ListItemsController. Be sure to keep this updated too.
  let(:valid_session) { {} }

  describe "GET index" do
    it "assigns all list_items as @list_items" do
      list_item = ListItem.create! valid_attributes
      get :index, project_id: 2, {}, valid_session
      assigns(:list_items).should eq([list_item])
    end
  end

ルート.rb:

  resources :projects do
    member do
      match "list_items"
    end
  end

注: - rpec テスト自体を project_id を含めるように変更しようとしましたが、役に立ちませんでした。- フィクスチャの生成に Factory Girl を使用しています (これが関連しているかどうかはわかりません)

ご協力いただきありがとうございます!

4

1 に答える 1

4

まず、実行rake routesして、どのようなルートが存在するかを確認します。

あなたのルートにあるものによるとProjectsController、 action を持つがあると思いますlist_items。このアクションは で利用できます/projects/:id/list_items

今、私はあなたが本当に欲しいものについて理論化することしかできませんが、推測します.

/projects/:project_id/list_itemsにルーティングしたい場合は、ルートをlist_items#index次のように変更する必要があります。

resources :projects do
    resources :list_items
end

を実行することで確認できますrake routes

次に、ルーティング仕様でアサーションを修正します。

get("/projects/23/list_items").should route_to("list_items#index", :project_id => "23")

RSpec v2.14+ 期待の更新

expect(:get => "/projects/23/list_items").to route_to("list_items#index", :project_id => "23")
于 2013-07-15T19:27:51.183 に答える