I'm learning how to write a NodeJS module that works against a vendor's REST API. The critical code for the module itself is written, but now I'm trying to learn how to test it all properly. Currently I'm using MochaJS and ChaiJS for the testing framework. In one test I create a user which returns a random ID, which I need to save. Then later I want to use said ID value and test the user deletion.
Here's the current code that doesn't work:
var names = require('./names.json');
var ids = [];
describe('users', function() {
describe('addUser', function (){
it('should create ' + names[0].firstname, function (done){
this.slow(3000); this.timeout(10000);
api.addUser(names[0],function(x){
x.should.have.property('id').with.length.of.at.least(2);
ids.push(x.id);
done();
});
});
it('should create ' + names[1].firstname, function (done){
this.slow(3000); this.timeout(10000);
api.addUser(names[1],function(x){
x.should.have.property('activated').and.equal(true);
ids.push(x.id);
done();
});
});
});
describe('deleteUser', function (){
for(var a=0;a<ids.length;a++){
it('should delete ' + ids[a], function (done){
api.deleteUser(ids[a],function(x){
x.should.have.property('id').and.equal(ids[a]);
done();
});
});
}
});
});
Even though ids
is scoped far outside the testing, the values are not saved. Now I've read other comments on stack overflow about this where the responders basically say "don't re-use values...something something waterfall failure". Which I understand but to me, that's Expected Functionality (TM). If for any reason (either my code or the vendors API) there is a failure and I cannot create a user, then obviously I will not be able to delete a user.
I want to put all this into Travis CI, so I cannot expect a specific user will always be there to delete unless my test framework creates is. I also have a limited number of users on the vendors system, so I need to clean up my testing. There are also other use cases (such as modifying an existing user) that I want to test.
Aucun commentaire:
Enregistrer un commentaire