Rails JSON API使用JSON中的PARAMS测试POST请求

时间:2022-11-26 21:59:15

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 ).

这是一个困扰我一段时间的问题。我正在构建一个API函数,它应该接收json格式的数据和json格式的响应。我的控制器测试运行良好(因为我对数据进行了抽象,从JSON中得到了解码,只有答案需要被解释)。

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"}}' )

我还知道这个函数运行得很好,因为我使用了curl来测试JSON参数,它工作得很好。(例如:curl -i -header“Accept: application/json”-header“Content-Type: application/json”-d '{“test”:{“email”:“andreo@benjamin”)。dk " } }”)

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.

但是很明显,我想写请求(特性)测试来自动测试它,我认为它们应该像curl一样工作,也就是。我的服务就像一个外部电话。这意味着我希望通过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.

我的问题是:我希望将参数和请求作为JSON发送是错误的,因为我将测试rails工作,因为这是它的责任?但是我希望看到我的代码对错误参数的健壮性,并希望尝试使用JSON。

something of this type:

一些这种类型的:

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.

你知道这怎么可能吗?如果你认为我测试错了请告诉我。

all the best,

愿一切都好!

Andre

安德烈

2 个解决方案

#1


19  

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.

我在这里的一个帖子上找到了我的答案(RSpec request test在post JSON params中合并了数组中的散列),我认为我做错的是请求的参数。

so this worked:

这工作:

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

#2


1  

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

#1


19  

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.

我在这里的一个帖子上找到了我的答案(RSpec request test在post JSON params中合并了数组中的散列),我认为我做错的是请求的参数。

so this worked:

这工作:

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

#2


1  

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