Search code examples
ruby-on-rails-4active-model-serializersjson-api

Key transform not working in test environment for active_model_serializers


I am using json_api adapter and haven't specified the Key transform explicitly, hence my app is using the Key transform provided by json_api which is :dashed

There is a variable in my serializer by name access_locked?. Below is the code snippet containing the serializer for admin model:

#app/serializers/admin_serializer.rb
class AdminSerializer < ActiveModel::Serializer
  attributes :id, :email, :access_locked?
end

In development environment, I am correctly getting the value in admin json as access-locked?, i.e. underscore_ being replaced by hyphen-

But my rspec test case is failing because the serialised admin contains access_locked? instead of access-locked?

Tried using ActiveModelSerializers.config.key_transform = :unaltered in both test.rb and active_model_serializers.rb but no success.

gem version is gem 'active_model_serializers', '0.10.0rc4' Here is the failing rspec:

#features/dashboard/admins/admins_index_spec.rb

scenario 'admin sees all other admins', :js => true do
    signin_admin(@admin.email, @admin.password)
    expect(page).to have_content I18n.t 'devise.sessions.signed_in'
    click_link 'Settings'
    click_link 'Admins'
    expect(page).to have_content @admin.email
    expect(page).to have_content @admin1.email
    expect(page).to have_content @admin2.email

    expect(page).to have_selector('#admin-row-1 .glyphicon-ban-circle')
#TODO failing because of http://stackoverflow.com/questions/36312019/key-transform-not-working-in-test-environment-for-active-model-serializers
    expect(page).to have_selector('#admin-row-2 .glyphicon-ok-circle')
    expect(page).to have_selector('#admin-row-3 .glyphicon-ban-circle')
    expect(page).to have_selector('#admin-row-1 .glyphicon-edit')
    expect(page).to have_selector('#admin-row-3 .glyphicon-trash')
    find('#admin-row-3 .glyphicon-edit').click
    expect(page).to have_selector('#admin-row-3 .glyphicon-check')
    expect(page).to have_selector('#admin-row-3 .glyphicon-remove')
    find('#admin-row-3 .glyphicon-check').click
    expect(page).to have_selector('#admin-row-3 .glyphicon-trash')
    find('#admin-row-1 .glyphicon-ban-circle').click
    expect(page).to have_css('.modal', text: 'Lock Admin?')
    find('.btn-primary.confirm').click
    expect(page).to have_content "Can't lock yourself."
    find('#admin-row-2 .glyphicon-ok-circle').click
    expect(page).to have_css('.modal', text: 'Unlock Admin?')
    find('.btn-primary.confirm').click
    expect(page).to have_selector('#admin-row-2 .glyphicon-ban-circle')
  end

Solution

  • Pointed the gem to master and it fixed this:

    gem 'active_model_serializers', github: 'rails-api/active_model_serializers', branch: :master