2016-02-18 75 views
0

我正在学习如何编写一个针对供应商的REST API的NodeJS模块。模块本身的关键代码是编写的,但现在我正试图学习如何正确地测试它。目前我正在使用MochaJS和ChaiJS作为测试框架。在一次测试中,我创建了一个返回随机ID的用户,我需要保存这个ID。然后,我想使用所述ID值并测试用户删除。如何将一个MochaJS测试的值传递给另一个?

下面是当前的代码不起作用:

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的作用范围远远超出了测试,数值不会被保存。现在我已经阅读了关于堆栈溢出的其他评论,其中响应者基本上说“不要重用值...某些事情瀑布故障”。我理解但对我来说,这是预期功能(TM)。如果由于任何原因(我的代码或供应商API)出现故障,并且我无法创建用户,那么显然我将无法删除用户。

我想把所有这些放到Travis CI中,所以我不能指望一个特定的用户将永远在那里删除,除非我的测试框架创建的是。我在供应商系统上的用户数量有限,所以我需要清理我的测试。还有其他用例(如修改现有用户),我想测试。

+0

在beforeEach中添加用户或者在调用delete之前有什么问题? –

+0

对于其他寻找一个更容易阅读的例子:[shakataganai/testing-mocha/test.js](https://github.com/ShakataGaNai/testing-mocha/blob/master/test.js) – Jon

回答

1

快速回答是您的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(); 
     }); 
     }); 
    }); 
    }); 
}); 

未经测试,无处可近,但应该工作。

+0

对于我特别的问题,你发现了。摩卡对事物如何工作的分析对我来说是新的 - 所以它不会在循环中运行测试。 它也是[mocha传递变量到下一个测试]的部分副本(http://stackoverflow.com/questions/20584233/mocha-pass-variable-to-the-next-test)。 – Jon

相关问题