@@ -66,6 +66,23 @@ def test_can_create_with_class_method
6666 assert_equal "Rails is Omakase" , article . title
6767 end
6868
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+
6986 def test_changed_attributes_empty_after_create_with_class_method
7087 stub_simple_creation
7188 article = Article . create ( {
@@ -206,7 +223,65 @@ def test_can_create_with_new_record_with_relationships_and_save
206223 assert article . persisted?
207224 assert_equal article . comments . length , 1
208225 assert_equal "1" , article . id
226+ end
209227
228+ def test_can_create_with_new_record_with_associated_relationships_and_save
229+ stub_request ( :post , "http://example.com/articles" )
230+ . with ( headers : { content_type : "application/vnd.api+json" , accept : "application/vnd.api+json" } , body : {
231+ data : {
232+ type : "articles" ,
233+ relationships : {
234+ author : {
235+ data : {
236+ id : 1 ,
237+ type : "authors"
238+ }
239+ }
240+ } ,
241+ attributes : {
242+ title : "Rails is Omakase"
243+ }
244+ }
245+ } . to_json )
246+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
247+ data : {
248+ type : "articles" ,
249+ id : "1" ,
250+ attributes : {
251+ title : "Rails is Omakase"
252+ } ,
253+ relationships : {
254+ author : {
255+ data : [
256+ {
257+ id : "1" ,
258+ type : "comments"
259+ }
260+ ]
261+ }
262+ }
263+ } ,
264+ included : [
265+ {
266+ id : "1" ,
267+ type : "authors" ,
268+ }
269+ ]
270+ } . to_json )
271+
272+ author_hash = {
273+ author : {
274+ data : {
275+ id : 1 ,
276+ type : 'authors'
277+ }
278+ }
279+ }
280+ article = Article . new ( { title : "Rails is Omakase" , "relationships" => author_hash } )
281+
282+ assert article . save
283+ assert article . persisted?
284+ assert_equal "1" , article . id
210285 end
211286
212287 def test_correct_create_with_nil_attribute_value
0 commit comments