Change in response from serializers after rails upgrade to 4.2.2 from 4.1.9

277 views Asked by At

I was upgrading my rails application from Rails 4.1.9 to 4.2.2.In rails 4.1.9 rspec was green.After the upgrade when i ran the test, i got a couple of rspec failures.

Rails version  = 4.2.2
Ruby version = 2.2.4
rspec = 3.0.0

Following are the gems I used for testing,
gem 'rspec-rails'
  gem 'rspec_junit_formatter', require: false, git: '[email protected]:sj26/rspec_junit_formatter.git'
  gem 'awesome_print'
  gem 'factory_girl'
  gem 'jasmine'
  gem 'jasmine-jquery-rails'
  gem 'quiet_assets'
  gem 'sequel'
  gem 'brakeman', require: false
  gem 'qunit-rails'
gem 'webmock', '~> 2.3.1' 
  gem 'capybara'
  gem 'simplecov', require: false
  gem 'simplecov-rcov', require: false
  gem 'cucumber-rails', require: false
  gem 'rack_session_access'
  gem 'poltergeist'
  gem "faker"
  gem 'shoulda-matchers'
  gem "shoulda-callback-matchers"

After rails upgrade to 4.2.2 I am getting a failure like,

 Failure/Error: response_json = JSON.parse(response.body)['credit_value_rating_types']
     TypeError:
       no implicit conversion of String into Integer

With rails version from 4.1.9 to 4.2.2, I am missing the key "['credit_value_rating_types']" in the response body.I am actually serializing the response using Activemodel Serializers.

I was unable to find the reason behind the failure.Any insights will be helpfull.

2

There are 2 answers

3
Danil Speransky On

It seems JSON.parse(response.body) returns an array and not a hash.

And so you get the error because arrays expect integers as indexes.

Check what you get in JSON.parse(response.body).

0
Charles Skariah On

After a day full of research I finally able to find the solution.The problem was created by activemodel serializer gem.Once I did the rails upgrade there was some potential issues with the response they are sending.

For some of the response the serializers where not even working.

The solution is to add the AMS gem explicitly and mention the stable branch

gem 'active_model_serializers', github: 'rails-api/active_model_serializers', branch: '0-8-stable'

bundle install

Solved the spec failure with that. You can find more details with the following link Github Issue