spec/grape_entity/exposure_spec.rb in grape-entity-0.6.1 vs spec/grape_entity/exposure_spec.rb in grape-entity-0.7.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require 'spec_helper'
describe Grape::Entity::Exposure do
let(:fresh_class) { Class.new(Grape::Entity) }
let(:model) { double(attributes) }
@@ -22,15 +24,25 @@
subject { fresh_class.find_exposure(:name) }
describe '#key' do
it 'returns the attribute if no :as is set' do
fresh_class.expose :name
- expect(subject.key).to eq :name
+ expect(subject.key(entity)).to eq :name
end
it 'returns the :as alias if one exists' do
fresh_class.expose :name, as: :nombre
- expect(subject.key).to eq :nombre
+ expect(subject.key(entity)).to eq :nombre
+ end
+
+ it 'returns the result if :as is a proc' do
+ fresh_class.expose :name, as: proc { object.name.reverse }
+ expect(subject.key(entity)).to eq(model.name.reverse)
+ end
+
+ it 'returns the result if :as is a lambda' do
+ fresh_class.expose :name, as: ->(obj, _opts) { obj.name.reverse }
+ expect(subject.key(entity)).to eq(model.name.reverse)
end
end
describe '#conditions_met?' do
it 'only passes through hash :if exposure if all attributes match' do