Ruby on rails 3 在集成测试中调用Rspec中的特定url
我试图在rspec中执行以下操作,我知道我不应该在集成测试中使用硬编码值:Ruby on rails 3 在集成测试中调用Rspec中的特定url,ruby-on-rails-3,rspec,Ruby On Rails 3,Rspec,我试图在rspec中执行以下操作,我知道我不应该在集成测试中使用硬编码值: get '/api/get-other-items?id=5109' 我能找到的最接近的是:但它只选择一个项目。我尝试了以下方法: get :controller => 'api', :action => 'get-other-items', :id => '5109' get 'api/get-other-items', :id => '5109' 这给了我一个错误的URI对象或UR
get '/api/get-other-items?id=5109'
我能找到的最接近的是:但它只选择一个项目。我尝试了以下方法:
get :controller => 'api', :action => 'get-other-items', :id => '5109'
get 'api/get-other-items', :id => '5109'
这给了我一个错误的URI对象或URI字符串
如果我以
get get_other_items, :id => '5109' q
我明白了
undefined local variable or method `get_other_items' for #<RSpec::Core::ExampleGroup::Nested_1:0x007f938fd65590>
我将如何执行这个简单的get
thx
更新答案1评论
以下是有问题的rspec代码:
it "testing getting other items for menu item" do
get get_other_items_path(:id => '5109')
JSON.parse(response.body)
puts response.body
周一1月23日$rspec请求/获取\u其他\u项目\u spec.rb
F
Failures:
1) GetOtherItems testing getting other items for menu item
Failure/Error: JSON.parse(response.body)
JSON::ParserError:
743: unexpected token at 'this is not found '
# ./requests/get_other_items_spec.rb:19:in `block (2 levels) in <top (required)>'
Finished in 13.57 seconds
1 example, 1 failure
Failed examples:
调用应该是get\u other\u items\u path:id=>5109-您需要将path添加到路由的名称中,或者如果您想要完整的url而不是相对路径,则需要添加url。您的路由看起来不像是将:id作为参数,如果您想要发送:id,我希望看到以下内容:
get_other_items /api/get-other-items/:id(.:format) {:controller=>"api", :action=>"get_other_items"}
resources :api do
get 'get_other_items', :on => :member
end
考虑到生成的路由的结构,我假设您使用match来定义路由,在您的路由中没有HTTP动词。要解决此问题,请尝试:
match 'api/get-other-items/:id' => 'api#get_other_items'
如果您使用的是restful路由,那么看起来您指定的是收集路由而不是成员路由。收集路由不采用:id,它被设计为返回许多您指定类型的记录。要使其成为成员路由,请使用以下命令:
get_other_items /api/get-other-items/:id(.:format) {:controller=>"api", :action=>"get_other_items"}
resources :api do
get 'get_other_items', :on => :member
end
一旦这项工作开始,您应该能够在ApiController规范的rspec中尝试以下操作:
如果这两个选项都不起作用,请发布您的routes条目,以便我们可以尝试其他操作。thx但仍然出现错误,看起来在我的404返回时,路由器中没有接收到这一错误。有没有一种方法可以输出它将“使用”的路由,即显然不进行http调用。你能发布routes.rb内容吗?thx-我知道这显然是我的问题:match'/api/get other items'=>“apiget\u other\u items”,:as=>:get_other_Items路由正常-您可以发布您获得的准确错误日志吗?上面更新了代码/问题,“this not found”是404