简体   繁体   English

使用对象参数和额外ID模拟POST请求

[英]Simulate a POST request with the object parameters and an extra id

I'm doing RSpec tests in a ROR application and I need to simulate a post action in order to test a controller. 我在ROR应用程序中进行RSpec测试,我需要模拟一个post动作以测试控制器。

The normal use is like this: 正常使用是这样的:

post :candidate, candidate: FactoryGirl.attributes_for(:candidate)

My problem is that I also need to pass an id that comes from the url. 我的问题是我还需要传递来自url的id。 The action url is this one: 动作网址是这样的:

candidates POST    /positions/:id/candidate(.:format)      positions#candidate

So what I need is something like this: 所以我需要的是这样的:

post :candidate, candidate: FactoryGirl.attributes_for(:candidate), id: position.id

How can I simulate a post request with object attributes and also an id? 如何使用对象属性和id模拟发布请求?

In the normal use the output in the rails server is this: 在正常使用中,rails服务器中的输出是这样的:

Parameters: {"utf8"=>"✓", "candidate"=>{"first_name"=>"John", "last_name"=>"Doe", "email"=>"john@gmail.com", "phone_number"=>"+351911111111", "skype_id"=>"qwe", "linkedin_url"=>"qwe", "why_work_with_us"=>"qwe", "resume_url"=>"qwe", "portfolio_url"=>"qwe"}, "id"=>"2"}

You can send all the required params in a params hash. 您可以使用params哈希发送所有必需的参数。 So, build a params hash and then pass that to the post call like this: 所以,构建一个params哈希,然后将其传递给post调用,如下所示:

let(:params) { { candidate: FactoryGirl.attributes_for(:candidate), id: position.id } }
post :candidate, params

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

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