File tree Expand file tree Collapse file tree 3 files changed +51
-4
lines changed Expand file tree Collapse file tree 3 files changed +51
-4
lines changed Original file line number Diff line number Diff line change @@ -107,11 +107,11 @@ def initialize(code, uri)
107
107
class RecordNotSaved < ServerError
108
108
attr_reader :record
109
109
110
- def initialize ( record = nil )
110
+ def initialize ( message = nil , record = nil )
111
111
@record = record
112
- msg = 'Record not saved'
113
-
114
- super nil , msg
112
+ end
113
+ def message
114
+ "Record not saved"
115
115
end
116
116
end
117
117
end
Original file line number Diff line number Diff line change @@ -66,6 +66,23 @@ def test_can_create_with_class_method
66
66
assert_equal "Rails is Omakase" , article . title
67
67
end
68
68
69
+ def test_failed_create!
70
+ stub_request ( :post , "http://example.com/users" )
71
+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
72
+ errors : [
73
+ {
74
+ status : "400" ,
75
+ title : "Error"
76
+ }
77
+ ]
78
+ } . to_json )
79
+
80
+ exception = assert_raises JsonApiClient ::Errors ::RecordNotSaved do
81
+ User . create! ( name : 'Hans' )
82
+ end
83
+ assert_equal "Record not saved" , exception . message
84
+ end
85
+
69
86
def test_changed_attributes_empty_after_create_with_class_method
70
87
stub_simple_creation
71
88
article = Article . create ( {
Original file line number Diff line number Diff line change @@ -36,6 +36,36 @@ def stub_simple_fetch
36
36
} . to_json )
37
37
end
38
38
39
+ def test_failed_update!
40
+ stub_simple_fetch
41
+ articles = Article . find ( 1 )
42
+ article = articles . first
43
+
44
+ stub_request ( :patch , "http://example.com/articles/1" )
45
+ . with ( headers : { content_type : "application/vnd.api+json" , accept : "application/vnd.api+json" } , body : {
46
+ data : {
47
+ id : "1" ,
48
+ type : "articles" ,
49
+ attributes : {
50
+ title : "Modified title" ,
51
+ }
52
+ }
53
+ } . to_json )
54
+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
55
+ errors : [
56
+ {
57
+ status : "400" ,
58
+ title : "Error"
59
+ }
60
+ ]
61
+ } . to_json )
62
+
63
+ exception = assert_raises JsonApiClient ::Errors ::RecordNotSaved do
64
+ article . update! ( title : 'Modified title' )
65
+ end
66
+ assert_equal "Record not saved" , exception . message
67
+ end
68
+
39
69
def test_can_update_found_record
40
70
stub_simple_fetch
41
71
articles = Article . find ( 1 )
You can’t perform that action at this time.
0 commit comments