@@ -37,9 +37,13 @@ def update
37
37
end
38
38
39
39
def destroy
40
- employee = Employee . find ( params [ :id ] )
41
- employee . destroy
42
- render_jsonapi ( employee , scope : false )
40
+ employee , success = jsonapi_destroy . to_a
41
+
42
+ if success
43
+ render json : { meta : { } }
44
+ else
45
+ render json : { error : employee . errors }
46
+ end
43
47
end
44
48
end
45
49
@@ -135,17 +139,37 @@ def do_put(id)
135
139
end
136
140
137
141
describe 'basic destroy' do
138
- let ( :employee ) { Employee . create! ( first_name : 'Joe' ) }
142
+ let! ( :employee ) { Employee . create! ( first_name : 'Joe' ) }
143
+
144
+ before do
145
+ allow_any_instance_of ( Employee )
146
+ . to receive ( :force_validation_error ) { force_validation_error }
147
+ end
148
+
149
+ let ( :force_validation_error ) { false }
139
150
140
151
it 'deletes the object' do
141
- delete :destroy , params : { id : employee . id }
152
+ expect {
153
+ delete :destroy , params : { id : employee . id }
154
+ } . to change { Employee . count } . by ( -1 )
142
155
expect { employee . reload } . to raise_error ( ActiveRecord ::RecordNotFound )
143
156
end
144
157
145
- it 'responds with object ' do
158
+ it 'responds with 200, empty meta ' do
146
159
delete :destroy , params : { id : employee . id }
147
- expect ( json_item [ 'id' ] ) . to eq ( employee . id . to_s )
148
- expect ( json_item [ 'first_name' ] ) . to eq ( 'Joe' )
160
+ expect ( response . status ) . to eq ( 200 )
161
+ expect ( json ) . to eq ( { 'meta' => { } } )
162
+ end
163
+
164
+ context 'when validation errors' do
165
+ let ( :force_validation_error ) { true }
166
+
167
+ it 'responds with correct error payload' do
168
+ expect {
169
+ delete :destroy , params : { id : employee . id }
170
+ } . to_not change { Employee . count }
171
+ expect ( json [ 'error' ] ) . to eq ( 'base' => [ 'Forced validation error' ] )
172
+ end
149
173
end
150
174
end
151
175
0 commit comments