-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing.model.test.js
88 lines (75 loc) · 2.14 KB
/
listing.model.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
//Interested in learning more about testing and the libraries used in this file?
//Check out - https://mochajs.org/#hooks - Structure of tests
//Check out - https://mochajs.org/#asynchronous-code - Asyn options
var should = require('should'),
mongoose = require('mongoose'),
Listing = require('./ListingSchema'),
config = require('./config');
var listing, id;
listing = {
code: "LBWEST",
name: "Library West",
coordinates: {
latitude: 29.6508246,
longitude: -82.3417565
},
address: "1545 W University Ave, Gainesville, FL 32603, United States"
}
describe('Listing Schema Unit Tests', function() {
before(function(done) {
mongoose.connect(config.db.uri, { useNewUrlParser: true });
mongoose.set('useCreateIndex', true);
mongoose.set('useFindAndModify', false);
done();
});
describe('Saving to database', function() {
/*
Mocha's default timeout for tests is 2000ms. To ensure that the tests do not fail
prematurely, we can increase the timeout setting with the method this.timeout()
*/
this.timeout(10000);
it('saves properly when code and name provided', function(done){
new Listing({
name: listing.name,
code: listing.code
}).save(function(err, listing){
should.not.exist(err);
id = listing._id;
done();
});
});
it('saves properly when all three properties provided', function(done){
new Listing(listing).save(function(err, listing){
should.not.exist(err);
id = listing._id;
done();
});
});
it('throws an error when name not provided', function(done){
new Listing({
code: listing.code
}).save(function(err){
should.exist(err);
done();
})
});
it('throws an error when code not provided', function(done){
new Listing({
name: listing.name
}).save(function(err){
should.exist(err);
done();
})
});
});
afterEach(function(done) {
if(id) {
Listing.deleteOne({ _id: id }).exec(function() {
id = null;
done();
});
} else {
done();
}
});
});