gpt4 book ai didi

ruby-on-rails - Rspec 格式将参数发布到字符串值

转载 作者:数据小太阳 更新时间:2023-10-29 08:18:14 26 4
gpt4 key购买 nike

当我发送一个对象 json 时,其中的所有字段都更改为字符串,破坏了我在 Controller 中的验证,并且出现以下错误。

Api::V1::BillsController POST #create when logged in 
Failure/Error: post :create, { bill: bill_attributes }
Apipie::ParamInvalid:
Invalid parameter 'value' value "41.64794235693306": Must be Float
# ./app/controllers/concerns/exception_aspects.rb:4:in exception_wrapper
# ./spec/controllers/api/v1/bills_controller_spec.rb:135:in block (4 levels) in <top (required)>

我的测试我尝试指示 request.headers['Content-Type'] = 'application/json'

let(:bill_attributes) { FactoryGirl.attributes_for :bill }

before(:each) do
request.headers['Content-Type'] = 'application/json'
post :create, { bill: bill_attributes }
end

it "when is valid description" do
expect(json_response[:description]).to eq(bill_attributes[:description])
end

我的工厂是

FactoryGirl.define do
factory :bill do
description { FFaker::Lorem.phrase }
value { (rand() * 100).to_f }
end
end

我的 Controller 验证是

api :POST, "/bills", "Add a new bill to an event"
description "Add a new bill"
param :bill, Hash, :required => true, :action_aware => true do
param :description, String, "Bill description"
param :bill_photo, Hash, :required => false do
param :base64image, String, "Base 64 image file"
end
param :value, Float, "Amount of the bill"
end

我尝试将 validation :value 从 Float 更改为 :number 但问题仍然存在

我正在使用 rails 4.2.3rspec 3.3.0

最佳答案

post :create, params: {}, as: :json

这在 Rails 5.0.3 和 rspec-rails 3.6.0 中有效

Rails Controller 规范现在继承自 ActionDispatch::IntegrationTest 而不是 ActionController::TestCase。但是 RSpec Controller 规范仍然使用已弃用的 ActionController::TestCase。 Relevant Rails commit here

关于ruby-on-rails - Rspec 格式将参数发布到字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com