Skip to content

Commit c249809

Browse files
author
Tilo Sloboda
committed
adding tests
1 parent bbf1747 commit c249809

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

lib/json_api_client/errors.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ def initialize(code, uri)
107107
class RecordNotSaved < ServerError
108108
attr_reader :record
109109

110-
def initialize(record = nil)
110+
def initialize(message = nil, record = nil)
111111
@record = record
112-
msg = 'Record not saved'
113-
114-
super nil, msg
112+
end
113+
def message
114+
"Record not saved"
115115
end
116116
end
117117
end

test/unit/creation_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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({

test/unit/updating_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,36 @@ def stub_simple_fetch
3636
}.to_json)
3737
end
3838

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+
3969
def test_can_update_found_record
4070
stub_simple_fetch
4171
articles = Article.find(1)

0 commit comments

Comments
 (0)