Skip to content

Commit 8192567

Browse files
authored
Add support to match just a subset of meta (#22)
* Modify have_meta to include an exactly chain * Updated README.md to include have_meta exactly
1 parent 8ea78c1 commit 8192567

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Available matchers:
4242
* `expect(document['data']).to have_link(:self).with_value('http://api.example.com/users/12')`
4343
* `expect(document).to have_meta`
4444
* `expect(document).to have_meta('foo' => 'bar')`
45+
* `expect(document).to have_meta('foo' => 'bar', 'fum' => 'baz').exactly`
4546
* `expect(document).to have_jsonapi_object`
4647
* `expect(document).to have_jsonapi_object('version' => '1.0')`
4748

lib/jsonapi/rspec/meta.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@ module Meta
44
::RSpec::Matchers.define :have_meta do |val|
55
match do |actual|
66
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
7+
return false unless actual.key?('meta')
8+
return true unless val
9+
710
val = JSONAPI::RSpec.as_indifferent_hash(val)
11+
return false unless val <= actual['meta']
12+
13+
!@exactly || (@exactly && val.size == actual['meta'].size)
14+
end
815

9-
actual.key?('meta') && (!val || actual['meta'] == val)
16+
chain :exactly do
17+
@exactly = true
1018
end
1119
end
1220
end

spec/jsonapi/meta_spec.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
require 'spec_helper'
22

33
RSpec.describe JSONAPI::RSpec, '#have_meta' do
4+
let(:doc) do
5+
{
6+
'meta' => {
7+
'one' => 'I',
8+
'two' => 'II',
9+
'three' => 'III'
10+
}
11+
}
12+
end
413
context 'when providing no value' do
514
it 'succeeds when meta is present' do
6-
expect('meta' => {}).to have_meta
15+
expect(doc).to have_meta
716
end
817

918
it 'fails when meta is absent' do
@@ -17,16 +26,24 @@
1726
after(:all) { ::RSpec.configuration.jsonapi_indifferent_hash = false }
1827

1928
it do
20-
expect('meta' => { 'foo' => 'bar' }).to have_meta(foo: :bar)
29+
expect(doc).to have_meta(one: 'I')
2130
end
2231
end
2332

24-
it 'succeeds when meta matches' do
25-
expect('meta' => { foo: 'bar' }).to have_meta(foo: 'bar')
33+
it 'succeeds when meta includes the value' do
34+
expect(doc).to have_meta('one' => 'I')
35+
end
36+
37+
it 'fails when meta does not include the value' do
38+
expect(doc).not_to have_meta('one' => 'II')
39+
end
40+
41+
it 'succeeds when meta exactly matches the value' do
42+
expect(doc).to have_meta({ 'one' => 'I', 'two' => 'II', 'three' => 'III' }).exactly
2643
end
2744

28-
it 'fails when meta mismatches' do
29-
expect('meta' => { foo: 'bar' }).not_to have_meta(bar: 'baz')
45+
it 'succeeds when meta does not exactly match the value' do
46+
expect(doc).not_to have_meta({ 'one' => 'foo', 'two' => 'II', 'three' => 'III' }).exactly
3047
end
3148

3249
it 'fails when meta is absent' do

0 commit comments

Comments
 (0)