首页 > 解决方案 > APIpie 接收整数参数作为字符串

问题描述

我有以下 RSpec 测试:

 it 'should not list alerts, since I do not have access to this model' do
   get :index, params: { model_id: @model2.id, workspace_id: @workspace.id }, as: :json
   expect(response).to have_http_status(:forbidden)
 end

它失败了,因为 Apipie 抱怨workspace_id它实际上不是一个字符串,它是一个整数。我调试了电话,检查了@workspaceid绝对是一个整数。

我现在正在将应用程序迁移到 Rails 5.2.0(以前的 Rails 4)时看到了这个问题。

有没有人见过这样的东西?

标签: ruby-on-railsrspecruby-on-rails-5rspec-railsapipie

解决方案


GET您尝试发送一些有效负载时,请求不包含正文。在GET请求的情况下,所有参数都作为 url 查询(例如/index?model_id=1&workspace_id=1)传递,并且所有参数都是字符串。

您在这里有两个选择:

  • 更改GETPOST,它将允许带有正文的请求。
  • 在操作中将字符串转换为整数。

推荐阅读