Skip to content

Commit 3bfa352

Browse files
author
Lee Richmond
committed
Add support for matching booleans
1 parent 83e20de commit 3bfa352

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/jsonapi_spec_helpers/matchers.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
RSpec::Matchers.define :match_type do |attribute, type|
1616
match do |actual|
17-
actual.is_a?(type)
17+
if type.is_a?(Array)
18+
type.any? { |t| actual.is_a?(t) }
19+
else
20+
actual.is_a?(type)
21+
end
1822
end
1923

2024
failure_message do |actual|

spec/assert_payload_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,34 @@
6464
end
6565
end
6666

67+
context 'when a condition that matches multiple types' do
68+
it 'passes assertion if one of the types passes' do
69+
json['data']['attributes']['is_foo'] = false
70+
allow(post_record).to receive(:is_foo) { false }
71+
expect {
72+
assert_payload(:post, post_record, json_item) do
73+
key(:is_foo, [TrueClass, FalseClass])
74+
end
75+
}.to_not raise_error
76+
77+
json['data']['attributes']['is_foo'] = true
78+
allow(post_record).to receive(:is_foo) { true }
79+
expect {
80+
assert_payload(:post, post_record, json_item) do
81+
key(:is_foo, [TrueClass, FalseClass])
82+
end
83+
}.to_not raise_error
84+
85+
json['data']['attributes']['is_foo'] = 'true'
86+
allow(post_record).to receive(:is_foo) { 'true' }
87+
expect {
88+
assert_payload(:post, post_record, json_item) do
89+
key(:is_foo, [TrueClass, FalseClass])
90+
end
91+
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
92+
end
93+
end
94+
6795
context 'when json value does not match payload value' do
6896
before do
6997
allow(post_record).to receive(:title) { 'foo' }

0 commit comments

Comments
 (0)