@@ -121,17 +121,41 @@ describe('Controller: Movie', () => {
121121
122122 sinon . assert . calledWith ( response . send ) ;
123123 } ) ;
124+
125+ it ( 'should return a 422 when an error occurs' , async ( ) => {
126+ const response = {
127+ send : sinon . spy ( ) ,
128+ status : sinon . stub ( ) ,
129+ } ;
130+
131+ class fakeMovie {
132+ save ( ) { }
133+ }
134+
135+ response . status . withArgs ( 422 ) . returns ( response ) ;
136+
137+ sinon
138+ . stub ( fakeMovie . prototype , 'save' )
139+ . withArgs ( )
140+ . rejects ( { message : 'Error' } ) ;
141+
142+ const movieController = new MovieController ( fakeMovie ) ;
143+
144+ await movieController . create ( defaultMovie , response ) ;
145+ sinon . assert . calledWith ( response . status , 422 ) ;
146+ } ) ;
124147 } ) ;
125148
126149 describe ( 'update()' , ( ) => {
150+ const fakeId = 'fake-id' ;
151+ const updatedMovie = {
152+ _id : fakeId ,
153+ name : 'Updated movie' ,
154+ description : 'Updated description' ,
155+ year : 2020 ,
156+ } ;
157+
127158 it ( 'should respond with 200 when the movie has been updated' , async ( ) => {
128- const fakeId = 'fake-id' ;
129- const updatedMovie = {
130- _id : fakeId ,
131- name : 'Updated movie' ,
132- description : 'Updated description' ,
133- year : 2020 ,
134- } ;
135159 const request = {
136160 params : {
137161 id : fakeId ,
@@ -151,11 +175,42 @@ describe('Controller: Movie', () => {
151175 updateOneStub
152176 . withArgs ( { _id : fakeId } , updatedMovie )
153177 . resolves ( updatedMovie ) ;
178+
154179 const movieController = new MovieController ( fakeMovie ) ;
155180
156181 await movieController . update ( request , response ) ;
157182 sinon . assert . calledWith ( response . sendStatus , 200 ) ;
158183 } ) ;
184+
185+ it ( 'should return a 422 when an error occurs' , async ( ) => {
186+ const request = {
187+ params : {
188+ id : fakeId ,
189+ } ,
190+ body : updatedMovie ,
191+ } ;
192+ const response = {
193+ send : sinon . spy ( ) ,
194+ status : sinon . stub ( ) ,
195+ } ;
196+
197+ class fakeMovie {
198+ static updateOne ( ) { }
199+ }
200+
201+ const updateOneStub = sinon . stub ( fakeMovie , 'updateOne' ) ;
202+
203+ updateOneStub
204+ . withArgs ( { _id : fakeId } , updatedMovie )
205+ . rejects ( { message : 'Error' } ) ;
206+
207+ response . status . withArgs ( 422 ) . returns ( response ) ;
208+
209+ const movieController = new MovieController ( fakeMovie ) ;
210+
211+ await movieController . update ( request , response ) ;
212+ sinon . assert . calledWith ( response . send , 'Error' ) ;
213+ } ) ;
159214 } ) ;
160215
161216 describe ( 'delete()' , ( ) => {
@@ -184,5 +239,33 @@ describe('Controller: Movie', () => {
184239
185240 sinon . assert . calledWith ( response . sendStatus , 204 ) ;
186241 } ) ;
242+
243+ it ( 'should return a 400 when an error occurs' , async ( ) => {
244+ const fakeId = 'fake-id' ;
245+ const request = {
246+ params : {
247+ id : fakeId ,
248+ } ,
249+ } ;
250+
251+ const response = {
252+ send : sinon . spy ( ) ,
253+ status : sinon . stub ( ) ,
254+ } ;
255+
256+ class fakeMovie {
257+ static deleteOne ( ) { }
258+ }
259+
260+ const deleteOneStub = sinon . stub ( fakeMovie , 'deleteOne' ) ;
261+
262+ deleteOneStub . withArgs ( { _id : fakeId } ) . rejects ( { message : 'Error' } ) ;
263+ response . status . withArgs ( 400 ) . returns ( response ) ;
264+
265+ const movieController = new MovieController ( fakeMovie ) ;
266+
267+ await movieController . delete ( request , response ) ;
268+ sinon . assert . calledWith ( response . send , 'Error' ) ;
269+ } ) ;
187270 } ) ;
188271} ) ;
0 commit comments