当它应该失败时用 mocha 测试成功

Test succeeding with mocha when it should fail

当我执行我的 mocha 测试脚本时,有几件事我并没有真正得到:

我正在测试大约 20 个请求,有些测试在不应该通过的时候通过了。例如,我想检索欧洲的国家,结果如下:

[ {name:'Germany',
   code:'de'},
  {name:'Spain',
   code:'es'},
...]


describe('get v2/continents/EU', function() {
  it('should return the country name', function(done) {
    options.path = "v2/continents/EU";
    http.get(options, function(res) {
      expect(res.statusCode).to.equal(200);
      var body = '';
      res.on('data', function(chunk) {
        body += chunk;
      });
      res.on('end', function() {
        var json = JSON.parse(body);
        expect(json.result[0].name).to.equal('France'); //This should fail
      });
      done();
    })
  })
})

我在列表中检索到的第一个国家是德国,而不是法国,但测试仍然通过,我不知道为什么,我做错了什么?

将您调用的 done() 移动到当前测试结束时,以便它在 res.on('end', ...) 的处理程序中:就在您的 expect 调用之后。

目前,您告诉 Mocha 测试在实际结束之前就结束了,但实际上您希望它仅在获得结果并进行测试后才结束。