我如何将值从一个 MochaJS 测试传递到另一个?
How do I carry values from one MochaJS test to another?
我正在学习如何编写针对供应商的 REST API 的 NodeJS 模块。模块本身的关键代码已经编写,但现在我正在尝试学习如何正确地测试它。目前我正在使用 MochaJS 和 ChaiJS 作为测试框架。在一个测试中,我创建了一个 returns 随机 ID 的用户,我需要保存它。然后稍后我想使用所述ID值并测试用户删除。
这是当前不起作用的代码:
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();
});
});
}
});
});
即使 ids
的范围远远超出测试范围,这些值也不会被保存。现在我已经阅读了关于堆栈溢出的其他评论,响应者基本上说 "don't re-use values...something something waterfall failure"。我理解,但对我来说,这是预期功能 (TM)。如果出于任何原因(我的代码或供应商 API)出现故障并且我无法创建用户,那么显然我将无法删除用户。
我想把所有这些都放到 Travis CI 中,所以我不能指望特定用户总是在那里删除,除非我的测试框架创建了。我在供应商系统上的用户数量也有限,所以我需要清理我的测试。我还想测试其他用例(例如修改现有用户)。
快速回答是您的 for
循环永远不会循环。
当测试文件被解析时,for 循环(在任何测试有 运行 之前,因此在你可以将任何东西推入 ids
之前)执行,因为 ids
是空的,没有工作要做。
为了证明这一点,将您的代码调整为:
describe('deleteUser', function (){
console.log("How many IDs?", id);
for(var a=0;a<ids.length;a++){
console.log("This will not be seen...");
it('should delete ' + ids[a], function (done){
api.deleteUser(ids[a],function(x){
x.should.have.property('id').and.equal(ids[a]);
done();
});
});
}
});
解决这个问题最简单的方法是不遍历 ID,而是一个接一个地删除两个用户,然后检查是否都成功:
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 (){
it('should delete users', function (done){
api.deleteUser(ids[0],function(x){
x.should.have.property('id').and.equal(ids[0]);
api.deleteUser(ids[1],function(x){
x.should.have.property('id').and.equal(ids[1]);
done();
});
});
});
});
});
未经测试,远非完美,但应该可以。
我正在学习如何编写针对供应商的 REST API 的 NodeJS 模块。模块本身的关键代码已经编写,但现在我正在尝试学习如何正确地测试它。目前我正在使用 MochaJS 和 ChaiJS 作为测试框架。在一个测试中,我创建了一个 returns 随机 ID 的用户,我需要保存它。然后稍后我想使用所述ID值并测试用户删除。
这是当前不起作用的代码:
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();
});
});
}
});
});
即使 ids
的范围远远超出测试范围,这些值也不会被保存。现在我已经阅读了关于堆栈溢出的其他评论,响应者基本上说 "don't re-use values...something something waterfall failure"。我理解,但对我来说,这是预期功能 (TM)。如果出于任何原因(我的代码或供应商 API)出现故障并且我无法创建用户,那么显然我将无法删除用户。
我想把所有这些都放到 Travis CI 中,所以我不能指望特定用户总是在那里删除,除非我的测试框架创建了。我在供应商系统上的用户数量也有限,所以我需要清理我的测试。我还想测试其他用例(例如修改现有用户)。
快速回答是您的 for
循环永远不会循环。
当测试文件被解析时,for 循环(在任何测试有 运行 之前,因此在你可以将任何东西推入 ids
之前)执行,因为 ids
是空的,没有工作要做。
为了证明这一点,将您的代码调整为:
describe('deleteUser', function (){
console.log("How many IDs?", id);
for(var a=0;a<ids.length;a++){
console.log("This will not be seen...");
it('should delete ' + ids[a], function (done){
api.deleteUser(ids[a],function(x){
x.should.have.property('id').and.equal(ids[a]);
done();
});
});
}
});
解决这个问题最简单的方法是不遍历 ID,而是一个接一个地删除两个用户,然后检查是否都成功:
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 (){
it('should delete users', function (done){
api.deleteUser(ids[0],function(x){
x.should.have.property('id').and.equal(ids[0]);
api.deleteUser(ids[1],function(x){
x.should.have.property('id').and.equal(ids[1]);
done();
});
});
});
});
});
未经测试,远非完美,但应该可以。