Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow matchers in expected value in with_data chain #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ Checking for an included resource:
expect(response_body['included'])
.to include(have_type('posts').and have_id('1'))
```

Combining `have_relationship.with_data` matcher with other matchers:

```ruby
expect(response_body)
.to have_relationship(:posts).with_data(include(have_id('1')))

expect(response_body)
.to have_relationship(:posts).with_data(all(have_type('post')))
```

## Contributing

Bug reports and pull requests are welcome on GitHub at
Expand Down
6 changes: 3 additions & 3 deletions lib/jsonapi/rspec/relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ module Relationships
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
return false unless (actual['relationships'] || {}).key?(rel.to_s)

!@data_set || actual['relationships'][rel.to_s]['data'] == @data_val
!@data_set || values_match?(@data_val, actual['relationships'][rel.to_s]['data'])
end

chain :with_data do |val|
@data_set = true
@data_val = JSONAPI::RSpec.as_indifferent_hash(val)
@data_val = ::RSpec::Matchers.is_a_matcher?(val) ? val : JSONAPI::RSpec.as_indifferent_hash(val)
end

failure_message do |actual|
if (actual['relationships'] || {}).key?(rel.to_s)
"expected #{actual['relationships'][rel.to_s]} " \
"to have data #{@data_val}"
"to have data #{description_of(@data_val)}"
else
"expected #{actual} to have relationship #{rel}"
end
Expand Down
6 changes: 6 additions & 0 deletions spec/jsonapi/relationships_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
)
end

it do
expect(doc).to have_relationship('comments').with_data(
include(have_id('1').and(have_type('comment')))
)
end

context 'with jsonapi indifferent hash enabled' do
before(:all) { ::RSpec.configuration.jsonapi_indifferent_hash = true }
after(:all) { ::RSpec.configuration.jsonapi_indifferent_hash = false }
Expand Down