Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec format Post parameters to String values

When I send an object json all fields inside are changed to string, breaking my validation in the controller and i get the following error.

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

My test I try indicate 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

My factory is

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

My controller validations are

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

I try to change validation :value from Float to :number but the problem continues

I am using rails 4.2.3 and rspec 3.3.0

like image 357
German Avatar asked Nov 19 '15 15:11

German


1 Answers

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

This is what works in Rails 5.0.3 and rspec-rails 3.6.0

Rails controller specs now inherit from ActionDispatch::IntegrationTest instead of ActionController::TestCase. But RSpec controller specs still use ActionController::TestCase which is deprecated. Relevant Rails commit here

like image 114
RajeshT Avatar answered Oct 02 '22 21:10

RajeshT