Skip to content

Commit ef47800

Browse files
authored
Merge pull request #17 from richmolj/master
Run payload procs in spec context
2 parents 2ef4f0a + 5c5e3ae commit ef47800

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/jsonapi_spec_helpers.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def assert_payload(name, record, json, &blk)
3434
prc = options[:proc]
3535
if (expect(json).to have_payload_key(attribute, options[:allow_nil])) == true
3636
unless options[:allow_nil]
37-
expect(json[attribute.to_s]).to match_payload(attribute, prc.call(record))
37+
output = instance_exec(record, &prc)
38+
expect(json[attribute.to_s]).to match_payload(attribute, output)
3839

3940
if options[:type]
4041
expect(json[attribute.to_s]).to match_type(attribute, options[:type])

spec/assert_payload_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,30 @@
3737

3838
context 'when payload is valid' do
3939
let(:post_record) do
40-
double \
40+
attrs = {
4141
id: 1,
4242
title: 'post title',
4343
description: 'post description',
4444
views: 100
45+
}
46+
double(attrs).as_null_object
4547
end
4648

49+
let(:some_let_var) { 'foo' }
50+
4751
it 'passes assertion' do
4852
assert_payload(:post, post_record, json_item)
4953
end
5054

55+
it 'has access to spec context' do
56+
post_record.title = some_let_var
57+
item = json_item
58+
item['title'] = some_let_var
59+
assert_payload(:post, post_record, item) do
60+
key(:title, String) { some_let_var }
61+
end
62+
end
63+
5164
context 'when json value matches payload value, but wrong type' do
5265
before do
5366
json['data']['attributes']['views'] = '100'

0 commit comments

Comments
 (0)