Skip to content

Commit 470c865

Browse files
committed
Minor spec cleanup, moved classes from being globally defined to live in spec modules.
1 parent f5be163 commit 470c865

File tree

7 files changed

+53
-43
lines changed

7 files changed

+53
-43
lines changed

spec/grape/entity_spec.rb

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
require 'spec_helper'
22

33
describe Grape::Entity do
4-
let(:fresh_class){ Class.new(Grape::Entity) }
4+
let(:fresh_class) { Class.new(Grape::Entity) }
55

66
context 'class methods' do
7-
subject{ fresh_class }
7+
subject { fresh_class }
88

99
describe '.expose' do
1010
context 'multiple attributes' do
@@ -238,7 +238,7 @@
238238

239239
let(:model){ mock(attributes) }
240240

241-
let(:attributes){ {
241+
let(:attributes) { {
242242
:name => 'Bob Bobson',
243243
:email => '[email protected]',
244244
:birthday => Time.gm(2012, 2, 27),
@@ -286,17 +286,46 @@
286286
res.should_not have_key :non_existant_attribute2
287287
end
288288

289-
it 'should serialize embedded objects which respond to #serializable_hash' do
290-
fresh_class.expose :name, :embedded
291-
presenter = fresh_class.new(EmbeddedExampleWithOne.new)
292-
presenter.serializable_hash.should == {:name => "abc", :embedded => {:abc => "def"}}
293-
end
289+
context "#serializable_hash" do
290+
291+
module EntitySpec
292+
class EmbeddedExample
293+
def serializable_hash(opts = {})
294+
{ :abc => 'def' }
295+
end
296+
end
297+
class EmbeddedExampleWithMany
298+
def name
299+
"abc"
300+
end
301+
def embedded
302+
[ EmbeddedExample.new, EmbeddedExample.new ]
303+
end
304+
end
305+
class EmbeddedExampleWithOne
306+
def name
307+
"abc"
308+
end
309+
def embedded
310+
EmbeddedExample.new
311+
end
312+
end
313+
end
314+
315+
it 'should serialize embedded objects which respond to #serializable_hash' do
316+
fresh_class.expose :name, :embedded
317+
presenter = fresh_class.new(EntitySpec::EmbeddedExampleWithOne.new)
318+
presenter.serializable_hash.should == {:name => "abc", :embedded => {:abc => "def"}}
319+
end
294320

295-
it 'should serialize embedded arrays of objects which respond to #serializable_hash' do
296-
fresh_class.expose :name, :embedded
297-
presenter = fresh_class.new(EmbeddedExampleWithMany.new)
298-
presenter.serializable_hash.should == {:name => "abc", :embedded => [{:abc => "def"}, {:abc => "def"}]}
321+
it 'should serialize embedded arrays of objects which respond to #serializable_hash' do
322+
fresh_class.expose :name, :embedded
323+
presenter = fresh_class.new(EntitySpec::EmbeddedExampleWithMany.new)
324+
presenter.serializable_hash.should == {:name => "abc", :embedded => [{:abc => "def"}, {:abc => "def"}]}
325+
end
326+
299327
end
328+
300329
end
301330

302331
describe '#value_for' do
@@ -330,12 +359,21 @@ def timestamp(date)
330359
end
331360

332361
it 'should disable root key name for child representations' do
362+
363+
module EntitySpec
364+
class FriendEntity < Grape::Entity
365+
root 'friends', 'friend'
366+
expose :name, :email
367+
end
368+
end
369+
333370
fresh_class.class_eval do
334-
expose :friends, :using => FriendEntity
371+
expose :friends, :using => EntitySpec::FriendEntity
335372
end
373+
336374
rep = subject.send(:value_for, :friends)
337375
rep.should be_kind_of(Array)
338-
rep.reject{|r| r.is_a?(FriendEntity)}.should be_empty
376+
rep.reject{|r| r.is_a?(EntitySpec::FriendEntity)}.should be_empty
339377
rep.first.serializable_hash[:name].should == 'Friend 1'
340378
rep.last.serializable_hash[:name].should == 'Friend 2'
341379
end

spec/grape_spec.rb

Lines changed: 0 additions & 1 deletion
This file was deleted.

spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
require 'hashie/hash'
1919

20-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each do |file|
20+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each do |file|
2121
require file
2222
end
2323

spec/support/examples/embedded_example.rb

Lines changed: 0 additions & 5 deletions
This file was deleted.

spec/support/examples/embedded_example_with_many.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

spec/support/examples/embedded_example_with_one.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

spec/support/examples/friend_entity.rb

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)