简体   繁体   English

如何为自定义路由编写rspec get语句?

[英]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: 现在我想为/user/:id/items(.:format)添加一个路由,而不是将项目定义为嵌套资源,我使用成员路由:

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 . 问题是当我尝试为控制器编写rspec时,我不知道应该使用什么操作来访问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 但在执行rake规范之后:控制器说

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. 我正在使用Rails 3.1。 How can I use rspec to test this member route? 如何使用rspec测试此成员路由? Thanks. 谢谢。

Pretty sure it needs to be: 很确定它需要:

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

You need to pass :id , not :user_id 您需要传递:id ,而不是:user_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM