Rails JSON API 测试 POST 要求。 PARAMS 在 JSON

这是一个困扰我一段时间的问题。 我创建了一个函数 API, 应该接收数据 json 并回答B. json. 我的控制器测试正常工作/由于我从数据获得的事实中摘要已经解码了 JSON 只有答案必须被解释 /.

我也知道该函数工作正常,因为我使用了 curl 用参数测试它 JSON, 它可以很好。
/例如: curl - 收藏家 "可见的: application/json"- 头脑 "内容类型: application/json" -D. '{"test":{"email":"andreo@benjamin.dk"}}' /

但显然,我想写查询测试/功能/, 自动检查它,并且如我所见,他们应该以与此同时的方式工作 curl, 也就是说,落入我的服务,好像这是一个外部挑战。 这意味着我想传达参数 JSON 并得到答案。 我很困惑,因为我看到人们的所有例子都会像已经破解一样解释论点。

我的问题是:我遵循错误的邮资,想要发送参数和请求 JSON, 因为我会检查一下 rails 它是因为它是他的责任吗? 但我想看看我的代码如何稳定到错误的论点,我想尝试 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


你知道怎么可能? 请告诉我如果您认为我会检查错误。

一切顺利,

安德烈
已邀请:

知食

赞同来自:

我在这里发现了我的答案/
https://coderoad.ru/18337609/
/, 我认为我错误地关心请求论据。

所以它有效:


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

莫问

赞同来自:

describe '#create' do 
let/:email/ {'andre'}
let/:attrs/ {{email: email}}
let/:params/ {{format: :json, subscription: attrs}}

it "should return an error if there is no correct email" do
post "/magazine_subscriptions", params
end
end

要回复问题请先登录注册