Skip to content

Commit 3c58d5f

Browse files
committed
test cases on controler layer
1 parent 35dc937 commit 3c58d5f

File tree

5 files changed

+1312
-96
lines changed

5 files changed

+1312
-96
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"mocha.enabled": true
3+
}

test/server/controllers/course.spec.js

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,41 @@ describe('Testing the Course controller:', async () => {
270270
updateCourseStub.restore();
271271
});
272272

273+
it('failure - failed update course service call', async () => {
274+
const request = {
275+
body: {
276+
id: 'f6859199-400b-48db-9a74-9071514ca3d2',
277+
name: 'physics',
278+
},
279+
params: { id: 'f6859199-400b-48db-9a74-9071514ca3d2' },
280+
};
281+
282+
const response = {};
283+
const utilErrorSpy = sinon.spy();
284+
const utilSendSpy = sinon.spy();
285+
286+
Util.prototype.setError = utilErrorSpy;
287+
Util.prototype.send = utilSendSpy;
288+
289+
response.status = function status() {
290+
return { json() {} };
291+
};
292+
293+
294+
const updateCourseStub = sinon.stub(courseService, 'updateCourse').resolves(false);
295+
const body = {
296+
id: 'testID',
297+
name: 'testName',
298+
};
299+
request.body = body;
300+
await CourseController.updateCourse(request, response);
301+
sinon.assert.calledOnce(utilErrorSpy);
302+
sinon.assert.calledOnce(utilSendSpy);
303+
sinon.assert.calledOnce(updateCourseStub);
304+
updateCourseStub.calledOnceWith(body);
305+
updateCourseStub.restore();
306+
});
307+
273308
it('failure - should be not able to create course due to bad id', async () => {
274309
const request = {
275310
body: {
@@ -455,4 +490,112 @@ describe('Testing the Course controller:', async () => {
455490
getCourseStub.restore();
456491
});
457492
});
493+
describe('Testing the delete course:', async () => {
494+
it('success - should be able to delete course', async () => {
495+
const request = {
496+
params: { id: 'f6859199-400b-48db-9a74-9071514ca3d2' },
497+
};
498+
499+
const response = {};
500+
const utilErrorSpy = sinon.spy();
501+
const utilSendSpy = sinon.spy();
502+
503+
Util.prototype.setError = utilErrorSpy;
504+
Util.prototype.send = utilSendSpy;
505+
506+
response.status = function status() {
507+
return { json() {} };
508+
};
509+
510+
const body = {
511+
id: 'testID',
512+
name: 'testName',
513+
};
514+
const deleteCourseStub = sinon.stub(courseService, 'deleteCourse').resolves(body);
515+
await CourseController.deleteCourse(request, response);
516+
sinon.assert.notCalled(utilErrorSpy);
517+
sinon.assert.calledOnce(utilSendSpy);
518+
sinon.assert.calledOnce(deleteCourseStub);
519+
deleteCourseStub.calledOnceWith(body);
520+
deleteCourseStub.restore();
521+
});
522+
523+
it('failure - failed delete course service call', async () => {
524+
const request = {
525+
params: { id: 'f6859199-400b-48db-9a74-9071514ca3d2' },
526+
};
527+
528+
const response = {};
529+
const utilErrorSpy = sinon.spy();
530+
const utilSendSpy = sinon.spy();
531+
532+
Util.prototype.setError = utilErrorSpy;
533+
Util.prototype.send = utilSendSpy;
534+
535+
response.status = function status() {
536+
return { json() {} };
537+
};
538+
539+
540+
const deleteCourseStub = sinon.stub(courseService, 'deleteCourse').resolves(false);
541+
await CourseController.deleteCourse(request, response);
542+
sinon.assert.calledOnce(utilErrorSpy);
543+
sinon.assert.calledOnce(utilSendSpy);
544+
sinon.assert.calledOnce(deleteCourseStub);
545+
deleteCourseStub.restore();
546+
});
547+
548+
it('failure - should be not able to create course due to bad id', async () => {
549+
const request = {
550+
params: { id: 'ftest3d2' },
551+
};
552+
553+
const response = {};
554+
const utilErrorSpy = sinon.spy();
555+
const utilSendSpy = sinon.spy();
556+
557+
Util.prototype.setError = utilErrorSpy;
558+
Util.prototype.send = utilSendSpy;
559+
560+
response.status = function status() {
561+
return { json() {} };
562+
};
563+
564+
565+
const deleteCourseStub = sinon.stub(courseService, 'deleteCourse').resolves();
566+
await CourseController.deleteCourse(request, response);
567+
sinon.assert.calledOnce(utilErrorSpy);
568+
sinon.assert.calledOnce(utilSendSpy);
569+
deleteCourseStub.restore();
570+
});
571+
572+
it('failure - should be not able to delete course - bad request', async () => {
573+
const request = {
574+
params: {
575+
id: 'f6859199-400b-48db-9a74-9071514ca3d2',
576+
},
577+
};
578+
579+
const response = {};
580+
const utilErrorSpy = sinon.spy();
581+
const utilSendSpy = sinon.spy();
582+
583+
Util.prototype.setError = utilErrorSpy;
584+
Util.prototype.send = utilSendSpy;
585+
586+
response.status = function status() {
587+
return { json() {} };
588+
};
589+
590+
591+
const deleteCourseStub = sinon.stub(courseService, 'deleteCourse').rejects();
592+
593+
await CourseController.deleteCourse(request, response);
594+
sinon.assert.calledOnce(utilErrorSpy);
595+
sinon.assert.calledOnce(utilSendSpy);
596+
sinon.assert.calledOnce(deleteCourseStub);
597+
deleteCourseStub.calledOnceWith(request.params.id, request.body);
598+
deleteCourseStub.restore();
599+
});
600+
});
458601
});

0 commit comments

Comments
 (0)