spec/grape_entity/entity_spec.rb in grape-entity-0.8.1 vs spec/grape_entity/entity_spec.rb in grape-entity-0.8.2
- old
+ new
@@ -28,11 +28,13 @@
expect { subject.expose :name, as: :foo }.not_to raise_error
end
it 'makes sure that :format_with as a proc cannot be used with a block' do
# rubocop:disable Style/BlockDelimiters
+ # rubocop:disable Lint/EmptyBlock
expect { subject.expose :name, format_with: proc {} do p 'hi' end }.to raise_error ArgumentError
+ # rubocop:enable Lint/EmptyBlock
# rubocop:enable Style/BlockDelimiters
end
it 'makes sure unknown options are not silently ignored' do
expect { subject.expose :name, unknown: nil }.to raise_error ArgumentError
@@ -258,31 +260,54 @@
subject.expose(:size) { |_| self }
expect(subject.represent({}).value_for(:size)).to be_an_instance_of fresh_class
end
end
- context 'with block passed via &' do
- it 'with does not pass options when block is passed via &' do
- class SomeObject
- def method_without_args
- 'result'
- end
+ describe 'blocks' do
+ class SomeObject
+ def method_without_args
+ 'result'
end
+ end
- subject.expose :that_method_without_args do |object|
- object.method_without_args
+ describe 'with block passed in' do
+ specify do
+ subject.expose :that_method_without_args do |object|
+ object.method_without_args
+ end
+
+ object = SomeObject.new
+
+ value = subject.represent(object).value_for(:that_method_without_args)
+ expect(value).to eq('result')
end
+ end
- subject.expose :that_method_without_args_again, &:method_without_args
+ context 'with block passed in via &' do
+ if RUBY_VERSION.start_with?('3')
+ specify do
+ subject.expose :that_method_without_args, &:method_without_args
+ subject.expose :method_without_args, as: :that_method_without_args_again
- object = SomeObject.new
+ object = SomeObject.new
+ expect do
+ subject.represent(object).value_for(:that_method_without_args)
+ end.to raise_error Grape::Entity::Deprecated
- value = subject.represent(object).value_for(:that_method_without_args)
- expect(value).to eq('result')
+ value2 = subject.represent(object).value_for(:that_method_without_args_again)
+ expect(value2).to eq('result')
+ end
+ else
+ specify do
+ subject.expose :that_method_without_args_again, &:method_without_args
- value2 = subject.represent(object).value_for(:that_method_without_args_again)
- expect(value2).to eq('result')
+ object = SomeObject.new
+
+ value2 = subject.represent(object).value_for(:that_method_without_args_again)
+ expect(value2).to eq('result')
+ end
+ end
end
end
context 'with no parameters passed to the block' do
it 'adds a nested exposure' do
@@ -1691,13 +1716,15 @@
root 'friends', 'friend'
expose :name, :email
end
end
+ # rubocop:disable Lint/EmptyBlock
fresh_class.class_eval do
expose :first_friend, using: EntitySpec::FriendEntity do |_user, _opts|
end
end
+ # rubocop:enable Lint/EmptyBlock
rep = subject.value_for(:first_friend)
expect(rep).to be_kind_of EntitySpec::FriendEntity
expect(rep.serializable_hash).to be_nil
end