-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
98 lines (89 loc) · 1.94 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const mocha = require('mocha');
const sinon = require('sinon');
const mongoose = require('mongoose');
const { getAnimals, createAnimal, deleteAnimal } = require('./routeHandlers')
const { Animal } = require('./models');
xit('dummytest', function(){
const test = sinon.fake();
sinon.assert.notCalled(test);
})
let res = {};
let sandbox;
beforeEach(function(){
sandbox = sinon.createSandbox();
const spy = sinon.spy();
res = {
json: spy,
status: sinon.stub().returns({json: spy})
}
})
afterEach(function(){
sandbox.restore();
})
it('should get all the animals', function(){
let req = {
query: {}
}
let expectedResult = [
{
name: 'tiger',
predator: true,
age: 7,
_id: 1,
},
{
name: 'horse',
predator: false,
age: 10,
_id: 2,
},
{
name: 'echidna',
predator: true,
age: 2,
_id: 3,
},
]
sandbox.stub(Animal, 'find').yields(null, expectedResult);
getAnimals(req, res);
sinon.assert.calledWith(res.status, sinon.match(200))
})
it('should create an animal', function(){
let req = {
query: {
name: "Liger",
age: 16,
predator: true,
}
};
let expectedResult = {
name: "Liger",
age: 16,
predator: true,
_id: new mongoose.Types.ObjectId(),
};
sandbox.stub(Animal, 'create').yields(null, expectedResult);
createAnimal(req, res);
sinon.assert.calledWith(Animal.create, req.query);
sinon.assert.calledWith(
res.json,
sinon.match(expectedResult)
)
})
it('should delete an animal', function(){
const idToBeDeleted = new mongoose.Types.ObjectId()
let req = {
params: {
_id: idToBeDeleted
}
};
let expectedResult = {
name: "Liger",
age: 16,
predator: true,
_id: idToBeDeleted
};
sandbox.stub(Animal, 'findByIdAndDelete').yields(null, expectedResult);
deleteAnimal(req, res);
sinon.assert.calledWith(Animal.findByIdAndDelete, req.params._id);
})