Skip to content

Commit 02ff269

Browse files
committed
fixed the double issue and added formatters. Thanks @zephyr-dev and @cveneziani
1 parent 985fbaf commit 02ff269

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

CHANGELOG.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ Next Release
22
============
33
* add support for minitest
44

5+
1.0.1 (07/30/2015)
6+
============
7+
* added fix for double from @zephyr-dev
8+
* added formatters from @cveneziani
9+
510
1.0.0 (07/30/2014)
611
============
712
* move support from rspec 2.x to rspec 3.x

grape-entity-matchers.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ Gem::Specification.new do |s|
1515
s.rubyforge_project = "grape-entity-matchers"
1616

1717
s.add_runtime_dependency 'grape-entity', '~> 0.4.0'
18-
s.add_runtime_dependency 'rspec', '>= 3.0.0.beta'
18+
s.add_runtime_dependency 'rspec', '>= 3.2.0'
19+
1920

20-
2121
s.add_development_dependency 'rake'
2222
s.add_development_dependency 'maruku'
2323
s.add_development_dependency 'yard'

lib/grape_entity_matchers/represent_matcher.rb

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,39 @@ module RepresentMatcher
66
def represent(representable)
77
RepresentMatcher.new(representable)
88
end
9-
9+
1010
class RepresentMatcher
11+
include ::RSpec::Mocks::ExampleMethods
1112
def initialize(representable)
1213
@expected_representable = representable
1314
RSpec::Mocks::setup
1415
end
15-
16+
1617
def matches?(subject)
1718
@subject = subject
1819

1920
check_methods && verify_exposure && check_value
2021
end
21-
22+
2223
def as(representation)
2324
@actual_representation = representation
2425
self
2526
end
26-
27+
28+
def format_with(formatter)
29+
@formatter = formatter
30+
self
31+
end
32+
2733
def when(conditions)
2834
@conditions = conditions
2935
self
3036
end
31-
37+
3238
def using(other_entity)
3339
@other_entity = other_entity
34-
@represented_attribute = double("RepresentedAttribute")
35-
@other_entity.exposures.keys.each do |key|
40+
@represented_attribute = double("RepresentedAttribute")
41+
@other_entity.exposures.keys.each do |key|
3642
allow(@represented_attribute ).to receive(key).and_return( @other_entity.exposures[key].nil? ? :value : nil)
3743
end
3844
self
@@ -54,7 +60,7 @@ def failure_message
5460
message << "#{@subject} didn't return the correct value for #{@expected_representable}. (#{@serialized_hash[@actual_representation || @expected_representable] } != #{@represented_attribute || :value})" unless check_value
5561
message
5662
end
57-
63+
5864
def failure_message_when_negated
5965
message = ""
6066
message << "Didn't expect #{@subject} to expose #{@expected_representable} correctly: #{@subject.exposures[@expected_representable]} \n" if verify_exposure
@@ -74,28 +80,28 @@ def negative_failure_message
7480
def description
7581
"Ensures that #{@subject} properly obtains the value of #{@expected_representable} from a mock class."
7682
end
77-
83+
7884
private
7985

8086
def limit_exposure_to_method(entity, method)
81-
allow(entity).to receive(:valid_exposures).and_return(
87+
allow(entity).to receive(:valid_exposures).and_return(
8288
entity.exposures.slice(method)
8389
)
8490
end
85-
91+
8692
def check_methods
8793
@representee = double("RepresentedObject")
8894
@represented_attribute ||= :value
8995

90-
allow(@representee).to receive(@expected_representable).and_return(@represented_attribute)
96+
allow(@representee).to receive(@expected_representable).and_return(@represented_attribute)
9197
expect(@representee).to receive(@conditions.keys.first).and_return(@conditions.values.first) unless @conditions.nil?
92-
98+
9399
representation = @subject.represent(@representee)
94100

95101
@serialized_hash = if @root
96-
limit_exposure_to_method(representation[@root.to_s], @expected_representable)
102+
limit_exposure_to_method(representation[@root.to_s], @expected_representable)
97103
representation[@root.to_s].serializable_hash
98-
else
104+
else
99105
limit_exposure_to_method(representation, @expected_representable)
100106
representation.serializable_hash
101107
end
@@ -107,14 +113,15 @@ def check_methods
107113
# here one can use #{e} to construct an error message
108114
methods_called = false
109115
end
110-
116+
111117
methods_called
112118
end
113-
119+
114120
def verify_exposure
115121
hash = {}
116122
hash[:using] = @other_entity unless @other_entity.nil?
117123
hash[:as] = @actual_representation unless @actual_representation.nil?
124+
hash[:format_with] = @formatter if @formatter
118125

119126
if @conditions.nil?
120127
@subject.exposures[@expected_representable] == hash
@@ -124,12 +131,12 @@ def verify_exposure
124131
exposures.delete(:if) != nil && exposures == hash
125132
end
126133
end
127-
134+
128135
def check_value
129136
if @other_entity
130137
other_representation = @other_entity.represent(@represented_attribute)
131138

132-
other_representation.exposures.keys.each do |key|
139+
other_representation.exposures.keys.each do |key|
133140
allow(other_representation).to receive(key).and_return( other_representation.exposures[key].nil? ? :value : nil)
134141
end
135142

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module GrapeEntityMatchers
2-
VERSION = '1.0.0'
2+
VERSION = '1.0.1'
33
end

spec/grape_entity_matchers/represent_matcher_spec.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ class PetEntity < Grape::Entity
99
class Person
1010
include Grape::Entity::DSL
1111
entity :name, :age do
12-
expose :date_of_birth, :as => :birthday
12+
format_with(:date_formatter) do |date|
13+
date
14+
end
15+
16+
expose :date_of_birth, :as => :birthday, :format_with => :date_formatter
1317
expose :cat, :as => :feline, :using => PetEntity
1418
expose :dog, :using => PetEntity
1519
expose :secret, :if => lambda{ |person, options| person.authorized? }
@@ -39,7 +43,7 @@ class ItemEntity < Grape::Entity
3943

4044
it { is_expected.to represent :name}
4145

42-
it { is_expected.to represent(:date_of_birth).as(:birthday) }
46+
it { is_expected.to represent(:date_of_birth).as(:birthday).format_with(:date_formatter) }
4347

4448
it { is_expected.to_not represent(:t_shirt_size) }
4549
it { is_expected.to_not represent(:t_shirt_size).as(:birthday) }

0 commit comments

Comments
 (0)