ZelluX
ZelluX

Reputation: 72655

How to write rspec get statements for customized routes?

I have defined a user resource, and each user has several items. Now I want to add a route for /user/:id/items(.:format), instead of defining items as nested resource, I use member route:

resources :user do
  member do
    match "/items" => "items#index", :via => [:get]
  end
end

The problem is when I try to write rspec for the controller, I don't know what action should I use to access items#index. I tried

describe ItemsController do
  it "denies access to item index without login" do
    sign_out @user
    get "index", :user_id => @user.id, :format => :json
    response.code.should == "401"
  end
  # ...
end

But after executing rake spec:controllers it says

Failure/Error: get "index", :user_id => @user.id, :format => :json
ActionController::RoutingError:
  No route matches {:user_id=>"1", :format=>"json", :controller=>"items"}

I am using Rails 3.1. How can I use rspec to test this member route? Thanks.

Upvotes: 3

Views: 1376

Answers (1)

raid5ive
raid5ive

Reputation: 6642

Pretty sure it needs to be:

get :index, :id => @user.id, :format => :json

You need to pass :id, not :user_id

Upvotes: 2

Related Questions