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

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

问题描述

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"

As detailed in the webrat documentation:

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

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.

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

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

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

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