使用参数在JSON Rails的JSON API测试POST请求 [英] Rails JSON API testing POST request with PARAMS in JSON
问题描述
这是一个已经困扰了我一段时间的问题。我建立应该在JSON和JSON响应接收数据的API函数。我控制器测试运行正常(因为我抽象的数据JSON到达那里已经去code和唯一的答案必须是跨preTED)。
This is a problem that has been bothering me for some time. I am building an API function that should receive data in json and response in json. My controller tests run fine(Since I abstract that the data gets there already decode from JSON and only the answer needs to be interpreted ).
我也知道,函数,因为我已经使用卷曲用JSON参数测试它和它的作品完美运行良好。
(例如:卷曲-i --header接受:应用/ JSON--header内容类型:应用程序/ JSON-d{测试:{电子邮件:andreo@benjamin.dk}} ')
I Also know that the function runs fine since I have used curl to test it with JSON arguments and it works perfectly. (ex: curl -i --header "Accept: application/json" --header "Content-Type: application/json" -d '{"test":{"email":"andreo@benjamin.dk"}}' )
但很明显,我还想写请求(功能)测试,自动测试这个,我看它,他们应该工作完全一样卷曲,即路,打我的服务就像是一个外部呼叫。这意味着,我想传递参数的JSON和接收答案。我是pretty因为所有的例子我可以看到人们把参数,因为它已经是德$ C $光盘丢失。
But obviously I would like to write request(feature) tests to test this automatically and the way I see it they should work exactly like curl, i.e., hit my service like it was an external call. That means that I would like to pass the arguments in JSON and receive an answer. I am pretty lost since all the examples I can see people treat arguments as it was already decoded.
我的问题是:我在想发送的参数和要求作为一个JSON酮按照错误的premise,因为我将要测试的轨道工程,因为这是它的责任?但我想怎么看我的强劲code他错误的论点和想尝试使用JSON。
My question is: I am following a wrong premise in wanting to send the arguments and request as a JSON one since i will be testing that rails works, because this is its responsibility? But I would like to see how robust my code his to wrong arguments and would like to try with JSON.
这类型的东西:
it "should return an error if there is no correct email" do
params = {:subscription => {:email => "andre"}}
post "/magazine_subscriptions", { 'HTTP_ACCEPT' => "application/json", 'Content-Type' => 'application/json', 'RAW_POST_DATA' => params.to_json }
end
你知道这是怎么可能呢?并请让我知道,如果你觉得我的测试是错误的。
Do you know how this is possible? and please let me know if you think I am testing it wrong.
一切顺利,
安德烈
推荐答案
我发现我的答案在这里后(<一href=\"http://stackoverflow.com/questions/18337609/rspec-request-test-merges-hashes-in-array-in-post-json-params\">RSpec要求测试合并哈希在POST JSON PARAMS 阵列),我觉得我在做什么错关注的参数要求。
I found my answer on a post here(RSpec request test merges hashes in array in POST JSON params), I think what I was doing wrong concerned the arguments to the request.
所以这个工作:
it "should return an error if there is no correct email" do
params = {:subscription => {:email => "andre"}}
post "/magazine_subscriptions", params.to_json, {'ACCEPT' => "application/json", 'CONTENT_TYPE' => 'application/json'}
end
这篇关于使用参数在JSON Rails的JSON API测试POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!