ruby 测试post调用时Rails/Rspec JSON整数被转换为字符串

ergxz8rk  于 2023-08-04  发布在  Ruby
关注(0)|答案(4)|浏览(86)

我正在测试一个JSON请求到我们的API,它将用JSON响应。看起来JSON中的所有整数都被转换为字符串,因为我们将它们发布到控制器consideraction。

控制器

def consider
    binding.pry # binding no# 2 used to check the params after post from test.
    if ParametersValidator.is_valid?(params)
      application_handler = ApplicationHandler.new(request_interactor)
      render json: application_handler.result
    else
      render json: ParametersValidator.failed_params(params).to_json
    end
  end

字符串
ParamaterValidator验证传入数据的结构和类型。

测试

render_views
let(:json) { JSON.parse(response.body) }
..
..

it 'returns the result in the correct format for the AUTOMATIC APPROVE decision' do
  automatic_approve_params = relative_json_file(relative_file('automatic_approve_params'))
  expected_approve_params = {
    "status" => "accepted",
    "automated" => true,
    "rate" => 6,
    "amount" => 30000,
    "term" => 10,
    "pre_approved_amount" => 2500,
    "comments" => ""
  }
  @request.headers['HTTP_X_AUTH_SIG'] = Rails.application.secrets['authorization']['token']
  request.env["HTTP_ACCEPT"] = 'application/json'

  binding.pry  # binding no# 1 to inspect the params before post

  post :consider, automatic_approve_params, format: :json
  expect(json).to eq(expected_approve_params)
end

绑定号#1

{
 "student_id"=>1,
 "age"=>22,
 "name"=>"John",
 "age_range"=>"22-25",
 "criminal_record"=>false,
 "declared_bankrupt"=>false,
 "declared_insolvent"=>false,
 "declared_sequestrated"=>false,
 "defaulted_on_loan"=>false,
 "post_study_salary"=>100000000,
 "first_nationality"=>"PL",
 "second_nationality"=>"",
 "citizenship"=>"PL",
}

绑定号#2

{
 "student_id"=>"1",
 "age"=>"22",
 "name"=>"John",
 "age_range"=>"22-25",
 "criminal_record"=>false,
 "declared_bankrupt"=>false,
 "declared_insolvent"=>false,
 "declared_sequestrated"=>false,
 "defaulted_on_loan"=>false,
 "post_study_salary"=>"100000000",
 "first_nationality"=>"PL",
 "second_nationality"=>"",
 "citizenship"=>"PL",
}


测试日志显示请求是

Processing by Api::V1::CreditApplicationsController#consider as JSON


在post操作之前检查请求,你会看到参数是正常的,然后在控制器中,在我运行任何东西之前,我检查参数,它们都是字符串。
使用postman来测试API与JSON的工作原理与预期一样,但似乎rspec在发布到consider操作时会将所有参数转换为字符串。我读过几十篇文章,声称通过将format: :json添加到帖子操作中可以解决这个问题,但是我没有这样的运气。
我显然做错了什么,但我已经尝试了几乎所有我知道的。

ix0qys7i

ix0qys7i1#

在复制了您遇到的问题后,我设法使用以下方法在控制器规范中解决了它:
第一个月
在我的本地测试中,我删除了request.env["HTTP_ACCEPT"] = 'application/json',它仍然像你期望的那样工作。希望对你有帮助。

wvmv3b1j

wvmv3b1j2#

在Rails 5中,使用as: :json而不是format: :json,例如post :consider, params: automatic_approve_params, as: :json

vcudknz3

vcudknz33#

我们可以试试这个

post 'orders.json', JSON.dump(order: {boolean: true, integer: 123}), "CONTENT_TYPE" => "application/json"

字符串

gjmwrych

gjmwrych4#

其他的答案都对我不起作用,经过大量的尝试,我找到了这个问题的解决方案。如果传递用to_json转换的params,并将'CONTENT_TYPE' => 'application/json'作为env传递,则integer将按原样传递给控制器。

let(:params) do
  {
    down_payment: 10_000,
    asking_price: 100_000,
    payment_schedule: 'weekly',
    amortization_period: 5
  }
end

it 'works' do
  post '/', params.to_json, 'CONTENT_TYPE' => 'application/json'
end

字符串
这将按预期工作。

相关问题