Skip to content

Commit 44c12c0

Browse files
committed
completed model unit test
1 parent 452e7a4 commit 44c12c0

File tree

2 files changed

+106
-20
lines changed

2 files changed

+106
-20
lines changed

models/Users.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ var debug = require('debug')(collection);
1010
var schemaObject = {
1111
name: {
1212
type: 'String'
13+
},
14+
someOtherStringData: {
15+
type: 'String'
1316
}
1417
};
1518

test/models.js

Lines changed: 103 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ var User;
1515
// Testing The User Model
1616
describe('User Model',function(){
1717

18+
var id;
19+
var id2;
1820

1921
before(function(){ /* jslint ignore:line */
2022
User = proxyquire('../models/Users', {'mongoose': 'mongooseMock'});
@@ -23,12 +25,10 @@ describe('User Model',function(){
2325

2426
describe('Test CRUDS', function(){
2527
it('should save data', function(done){
26-
var cb = sinon.spy();
2728
var myuser = User.create({name: 'femi'});
2829

2930
myuser.then(function(res){
30-
cb();
31-
cb.should.have.been.calledOnce; /* jslint ignore:line */
31+
res.should.be.an.object; /* jslint ignore:line */
3232
done();
3333
})
3434
.catch(function(err){
@@ -37,12 +37,10 @@ describe('User Model',function(){
3737
});
3838

3939
it('should read data', function(done){
40-
var cb = sinon.spy();
4140
var myuser = User.findOne({name: 'femi'});
4241

4342
myuser.then(function(res){
44-
cb();
45-
cb.should.have.been.calledOnce; /* jslint ignore:line */
43+
res.should.be.an.object; /* jslint ignore:line */
4644
done();
4745
})
4846
.catch(function(err){
@@ -51,12 +49,10 @@ describe('User Model',function(){
5149
});
5250

5351
it('should read all data', function(done){
54-
var cb = sinon.spy();
5552
var myuser = User.find();
5653

5754
myuser.then(function(res){
58-
cb();
59-
cb.should.have.been.calledOnce; /* jslint ignore:line */
55+
res.should.be.an.array; /* jslint ignore:line */
6056
done();
6157
})
6258
.catch(function(err){
@@ -93,13 +89,11 @@ describe('User Model',function(){
9389
});
9490

9591
it('should search data', function(done){
96-
var cb = sinon.spy();
9792
// Search needs more work for more accuracy
9893
var myuser = User.search('femi');
9994

10095
myuser.then(function(res){
101-
cb();
102-
cb.should.have.been.calledOnce; /* jslint ignore:line */
96+
res.should.be.an.object; /* jslint ignore:line */
10397
done();
10498
})
10599
.catch(function(err){
@@ -108,14 +102,12 @@ describe('User Model',function(){
108102
});
109103

110104
it('should delete data', function(done){
111-
var cb = sinon.spy();
112105
var cb2 = sinon.spy();
113106
var ouruser = User.create([{name:'Olaolu'},{name: 'fola'},{name: 'bolu'}]);
114107
var myuser = User.deleteOne({name: 'bolu'});
115108

116109
ouruser.then(function(res){
117-
cb();
118-
cb.should.have.been.calledOnce; /* jslint ignore:line */
110+
res.should.be.an.object; /* jslint ignore:line */
119111
return myuser;
120112
}).then(function(res){
121113
cb2();
@@ -145,6 +137,7 @@ describe('User Model',function(){
145137
var myuser = User.create({name: 'this is for the gods'});
146138

147139
myuser.then(function(res){
140+
id = res._id;
148141
res.should.have.property('createdAt');
149142
done();
150143
})
@@ -155,9 +148,8 @@ describe('User Model',function(){
155148

156149
it('should add updatedAt', function(done){
157150
var myuser = User.create({name: 'i am a demigod!'});
158-
var id;
159151
myuser.then(function(res){
160-
id = res._id;
152+
id2 = res._id;
161153
return User.update({_id: id},{name: 'This is the titan'});
162154
})
163155
.then(function(res){
@@ -172,11 +164,102 @@ describe('User Model',function(){
172164
});
173165
});
174166

175-
// it('should tag database entries properly');
167+
it('should tag database entries properly', function(done){
168+
var myuser = User.create({name: 'femi',someOtherStringData: 'random stuff'});
169+
170+
myuser.then(function(res){
171+
res.tags.length.should.equal(2);/* jslint ignore:line */
172+
done();
173+
})
174+
.catch(function(err){
175+
done(err);
176+
});
177+
});
178+
179+
it('should count returned records', function(done){
180+
var myuser = User.count({name: 'This is the titan'});
181+
182+
myuser.then(function(res){
183+
res.should.be.a.number; /* jslint ignore:line */
184+
done();
185+
})
186+
.catch(function(err){
187+
done(err);
188+
});
189+
});
190+
191+
it('should find a record by id', function(done){
192+
var myuser = User.findById(id);
193+
194+
myuser.then(function(res){
195+
res.should.be.an.object; /* jslint ignore:line */
196+
done();
197+
})
198+
.catch(function(err){
199+
done(err);
200+
});
201+
});
202+
203+
it('should find a record by id and delete', function(done){
204+
var myuser = User.findByIdAndRemove(id2);
176205

177-
// it('should tag database entries properly');
206+
myuser.then(function(res){
207+
res.should.be.an.object; /* jslint ignore:line */
208+
done();
209+
})
210+
.catch(function(err){
211+
done(err);
212+
});
213+
});
214+
215+
it('should find a record by id and update', function(done){
216+
var myuser = User.findByIdAndUpdate(id,{name: 'fufu'});
217+
218+
myuser.then(function(res){
219+
res.should.be.an.object; /* jslint ignore:line */
220+
done();
221+
})
222+
.catch(function(err){
223+
done(err);
224+
});
225+
});
226+
227+
it('should find the first match from a query', function(done){
228+
var myuser = User.findOne({name: 'fufu'});
229+
230+
myuser.then(function(res){
231+
res.should.be.an.object; /* jslint ignore:line */
232+
done();
233+
})
234+
.catch(function(err){
235+
done(err);
236+
});
237+
});
238+
239+
it('should find the first match from a query and update', function(done){
240+
var myuser = User.findOneAndUpdate({name: 'fufu'},{name: 'funmi'});
241+
242+
myuser.then(function(res){
243+
res.should.be.an.object; /* jslint ignore:line */
244+
done();
245+
})
246+
.catch(function(err){
247+
done(err);
248+
});
249+
});
250+
251+
it('should find the first match from a query and delete', function(done){
252+
var myuser = User.findOneAndRemove({name: 'funmi'});
253+
254+
myuser.then(function(res){
255+
res.should.be.an.object; /* jslint ignore:line */
256+
done();
257+
})
258+
.catch(function(err){
259+
done(err);
260+
});
261+
});
178262

179-
// it('should tag database entries properly');
180263
});
181264
});
182265

0 commit comments

Comments
 (0)