File tree 4 files changed +61
-0
lines changed
4 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -46,6 +46,7 @@ Available matchers:
46
46
* ` expect(document).to have_meta('foo' => 'bar', 'fum' => 'baz').exactly `
47
47
* ` expect(document).to have_jsonapi_object `
48
48
* ` expect(document).to have_jsonapi_object('version' => '1.0') `
49
+ * ` expect(document).to have_error('status' => '422') `
49
50
50
51
### On matcher arguments...
51
52
Original file line number Diff line number Diff line change 8
8
require 'jsonapi/rspec/links'
9
9
require 'jsonapi/rspec/meta'
10
10
require 'jsonapi/rspec/jsonapi_object'
11
+ require 'jsonapi/rspec/errors'
11
12
12
13
RSpec . configure do |c |
13
14
c . add_setting :jsonapi_indifferent_hash , default : false
Original file line number Diff line number Diff line change
1
+ module JSONAPI
2
+ module RSpec
3
+ module Meta
4
+ ::RSpec ::Matchers . define :have_error do |error |
5
+ match do |actual |
6
+ actual = JSONAPI ::RSpec . as_indifferent_hash ( actual )
7
+ return false unless actual . key? ( 'errors' )
8
+ return false unless actual [ 'errors' ] . is_a? ( Array )
9
+
10
+ return true if actual [ 'errors' ] . any? && error . nil?
11
+
12
+ error = JSONAPI ::RSpec . as_indifferent_hash ( error )
13
+
14
+ actual [ 'errors' ] . any? { |actual_error | error <= actual_error }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ RSpec . describe JSONAPI ::RSpec , '#have_error' do
4
+ let ( :doc ) do
5
+ {
6
+ 'errors' => [
7
+ {
8
+ 'status' => '422' ,
9
+ 'source' => { 'pointer' => '/data/attributes/firstName' } ,
10
+ 'title' => 'Invalid Attribute' ,
11
+ 'detail' => 'First name must contain at least three characters.'
12
+ }
13
+ ]
14
+ }
15
+ end
16
+
17
+ context 'when providing no value' do
18
+ it 'succeeds when errors are present' do
19
+ expect ( doc ) . to have_error
20
+ end
21
+
22
+ it 'fails when errors are missing' do
23
+ expect ( { } ) . not_to have_error
24
+ expect ( { 'errors' => [ ] } ) . not_to have_error
25
+ end
26
+ end
27
+
28
+ context 'when providing a value' do
29
+ it do
30
+ expect ( doc ) . to have_error (
31
+ 'status' => '422' ,
32
+ 'source' => { 'pointer' => '/data/attributes/firstName' }
33
+ )
34
+ end
35
+
36
+ it 'fails when meta is absent' do
37
+ expect ( doc ) . not_to have_error ( { 'status' => '500' } )
38
+ end
39
+ end
40
+ end
You can’t perform that action at this time.
0 commit comments