我正在使用json_api
适配器并且未明确指定密钥转换,因此我的应用正在使用json_api
提供的密钥转换:dashed
我的序列化程序中有一个名称为access_locked?
的变量。
以下是包含管理模型序列化程序的代码段:
#app/serializers/admin_serializer.rb
class AdminSerializer < ActiveModel::Serializer
attributes :id, :email, :access_locked?
end
在开发环境中,我正确地将admin json中的值设为access-locked?
,即将下划线替换为连字符 -
但是我的rspec测试用例失败了,因为序列化管理员包含access_locked?
而不是access-locked?
在ActiveModelSerializers.config.key_transform = :unaltered
和test.rb
中使用active_model_serializers.rb
尝试但未成功。
宝石版本为gem 'active_model_serializers', '0.10.0rc4'
这是失败的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
答案 0 :(得分:1)
此时可能已解决,但以防万一......我认为你想使用ActiveModelSerializers.config.key_transform = :dash
而不是:unaltered
答案 1 :(得分:0)
指出宝石掌握并解决了这个问题:
gem 'active_model_serializers', github: 'rails-api/active_model_serializers', branch: :master