I'm not used to write unit tests, but want to improve myself when it comes to testing. Currently I'm writing an app (basically just a REST API) with a small SQLite database. How to write tests against the database if there's no data?
Assuming a GET request to /api/posts returns all posts as json
[
{ id: 1, title: 'Test' },
{ id: 2, title: 'Blah' }
]
So my test looks like
describe('GET /', function () {
it('should return some posts', function (done) {
request(server)
.get('/api/posts')
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
if(err) {
throw err;
}
res.body.should.be.instanceof(Array);
// ... hmmm?
done();
});
});
});
But how to check the data itself (assuming there are no posts in the database at this point)? I'd like to check if the attributes (title, ...) are given, if passed parameters (like limit, etc.) return expected results (e.g. limited reults, ...), etc..
Aucun commentaire:
Enregistrer un commentaire