Ger*_*man 2 ruby rspec ruby-on-rails rspec2 rspec-rails
当我发送一个对象json时,里面的所有字段都改为字符串,在控制器中断开我的验证,我得到以下错误.
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)>
Run Code Online (Sandbox Code Playgroud)
我尝试的测试表明 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
Run Code Online (Sandbox Code Playgroud)
我的工厂是
FactoryGirl.define do
factory :bill do
description { FFaker::Lorem.phrase }
value { (rand() * 100).to_f }
end
end
Run Code Online (Sandbox Code Playgroud)
我的控制器验证是
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
Run Code Online (Sandbox Code Playgroud)
我试图改变验证:从价值Float到:number,但问题仍然存在
我正在使用rails 4.2.3和rspec 3.3.0
post :create, params: {}, as: :json
Run Code Online (Sandbox Code Playgroud)
这适用于Rails 5.0.3和rspec-rails 3.6.0
Rails控制器规范现在继承自ActionDispatch :: IntegrationTest而不是ActionController :: TestCase.但是RSpec控制器规范仍然使用不推荐使用的ActionController :: TestCase. 相关的Rails在这里提交
我添加了 json 格式来发布请求,它的效果就像魅力一样
before(:each) do
post :create, { bill: bill_attributes, format: :json }
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2131 次 |
| 最近记录: |