Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit b6830ba

Browse files
committed
Better user model behavior and test teardown
1 parent fe71732 commit b6830ba

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

app/models/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var mongoose = require('mongoose'),
1515
var UserSchema = new Schema({
1616
name: String,
1717
email: String,
18-
username: String,
18+
username: {type: String, unique: true},
1919
provider: String,
2020
hashed_password: String,
2121
facebook: {},

test/article/model.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ describe('<Unit Test>', function() {
5252
});
5353

5454
afterEach(function(done) {
55+
Article.remove({});
56+
User.remove({});
57+
done();
58+
});
59+
after(function(done){
60+
Article.remove().exec();
61+
User.remove().exec();
5562
done();
5663
});
5764
});
58-
});
65+
});

test/user/model.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,33 @@ describe('<Unit Test>', function() {
1919
username: 'user',
2020
password: 'password'
2121
});
22+
user2 = new User({
23+
name: 'Full name',
24+
email: 'test@test.com',
25+
username: 'user',
26+
password: 'password'
27+
});
2228

2329
done();
2430
});
2531

2632
describe('Method Save', function() {
33+
it('should begin with no users', function(done){
34+
User.find({}, function(err,users){
35+
users.should.have.length(0);
36+
done();
37+
});
38+
});
39+
2740
it('should be able to save whithout problems', function(done) {
28-
return user.save(function(err) {
29-
should.not.exist(err);
30-
done();
41+
user.save(done);
42+
});
43+
44+
it('should fail to save an existing user again', function(done) {
45+
user.save();
46+
return user2.save(function(err){
47+
should.exist(err);
48+
done();
3149
});
3250
});
3351

@@ -41,7 +59,8 @@ describe('<Unit Test>', function() {
4159
});
4260

4361
after(function(done) {
62+
User.remove().exec();
4463
done();
4564
});
4665
});
47-
});
66+
});

0 commit comments

Comments
 (0)