从黄瓜发出POST请求以创建新对象的最佳方法是什么? [英] What is the best way to make a POST request from cucumber to create a new object?

查看:78
本文介绍了从黄瓜发出POST请求以创建新对象的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的几种库克场景,我想将用户发布到在params中带有对象的页面,以便Rails在数据库中创建一个新对象。是否有一种方便的方法可以将POST +参数发送给某个动作(使用黄瓜中的东西还是使用Rails),还是必须使用Net :: Http编写自己的函数?

For several of my cuke scenarios, I want to POST the user to a page with an object in the params, so that Rails will make a new object in the database. Is there a convenient way to send a POST+params to an action (either using something in cucumber or rails) or do I have to write my own function using Net::Http?

推荐答案

只需访问要向其发布一些参数的控制器即可创建对象。

Just visit the controller you want to post some parameters to in order to create an object.

您不需要表格来做到这一点。只需将值发布到正在处理这些发布的任何控制器上,然后继续进行测试。控制器关心的只是获取可使用的参数,但是它们已发布。

You do not need a form to do this. Just post the values to whatever controller is handling these posts and continue on with your testing. All the controller cares about is getting params to work with, however they get posted.

visit "/user", :post, :firstName => "Foo", :lastName => "Bar"

webrat文档

visit (url = nil, http_method = :get, data = {})

更新:除非我误认为水豚有更多或更小的Webrat被取代了,通常人们认为,黄瓜级测试应该使用实际的用户界面,而不是与界面交互的API。就是说,这仍然是可能的,而且我仍然很早就发现它对自己很有用

UPDATE: Unless I'm mistaken Capybara has more or less superseded Webrat and generally the there's an opinion that cucumber level tests should exercise the actual user interface, and not the API that interface interacts with. That said, it is still possible and I still find it useful myself when early on in a project I just want to test my controllers and figure out the UI later.

因此在Capybara中,具体取决于您的驱动程序,但假设机架/测试

So in Capybara, depending on your driver, but assuming rack/test:

page.driver.post('/user', { firstName: "Foo", lastName: "Bar" })

这篇关于从黄瓜发出POST请求以创建新对象的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆