it('should return error trying to save duplicate username', function(done){
var profile = {
username: 'vgheri',
password: 'test',
firstName: 'Valerio',
lastName: 'Gheri'
};
// once we have specified the info we want to send to the server via POST verb,
// we need to actually perform the action on the resource, in this case we want to
// POST on /api/profiles and we want to send some info
// We do this using the request object, requiring supertest!
request(url)
.post('/api/profiles')
.send(profile)
// end handles the response
.end(function(err, res){
if (err) {
throw err;
}
// this is should.js syntax, very clear
res.should.have.status(400);
done();
});
});
it('should correctly update an existing account', function(done){
var body = {
firstName: 'JP',
lastName: 'Berd'
};
request(url)
.put('/api/profiles/vgheri')
.send(body)
.expect('Content-Type', /json/)
.expect(200) //Status code
.end(function(err,res){
if (err) {
throw err;
}
// Should.js fluent syntax applied
res.body.should.have.property('_id');
res.body.firstName.should.equal('JP');
res.body.lastName.should.equal('Berd');
res.body.creationDate.should.not.equal(null);
done();
});
});
});
});
1
2
3
4
5
6
7
8
9
describe('#index', function(){
it('should get /topic/:tid 200', function(done){
request.get('/topic/' + support.testTopic._id)
.expect(200, function(err, res){
res.text.should.containEql('test topic content');
res.text.should.containEql('alsotang');
done(err);
});
});
涉及到插入的时候,之前写rspec也遇到类似的 的情况,可以通过测试count是否增加1。
Fun
mocha -R nyan and whole lots of other stuff.
1
2
3
4
5
6
1 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-__,------,
0 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-__| /\_/\
0 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_~|_( ^ .^)
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_""""
1 passing (141ms)
Chai
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.
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
Should
chai.should();
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.length(3);
tea.should.have.property('flavors')
.with.length(3);
Expect
var expect = chai.expect;
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.length(3);
expect(tea).to.have.property('flavors')
.with.length(3);
Assert
var assert = chai.assert;
assert.typeOf(foo, 'string');
assert.equal(foo, 'bar');
assert.lengthOf(foo, 3)
assert.property(tea, 'flavors');
assert.lengthOf(tea.flavors, 3);
SuperTest
Miscs
Be kind and don’t make developers hunt around in your docs to figure out how to run the tests, add a make test target to your Makefile - TJ
Nock
Nock is an HTTP mocking library that can record HTTP calls and play them back, so that you can either mock an API by hand, or just record actual HTTP calls and use their responses in future test runs. Our orchestrate.js library does the former, but I’ll show you how to do the latter.