【问题标题】:mocha testing causes a timeout errormocha 测试导致超时错误
【发布时间】:2014-02-04 01:30:22
【问题描述】:

不知道为什么会出错:

'use strict';

/**
 * Module dependencies.
 */
var should = require('should'),
    mongoose = require('mongoose'),
    Block = mongoose.model('Block');

//Globals
var block;

//The tests
describe('<Unit Test>', function() {
  describe('Model Block:', function() {
    beforeEach(function(done) {
      block = new Block({
        bits: "1d00ffff",
        block_fee: 0,
        blockreward: 50,
        coinbase: 50,
        difficulty: 1,
        fees_claimed: 0,
        fees_paid: 0,
        hash: "000000005c51de2031a895adc145ee2242e919a01c6d61fb222a54a54b4d3089",
        height: 13,
        merkleroot: "9962d5c704ec27243364cbe9d384808feeac1c15c35ac790dffd1e929829b271",
        nextblockhash: "0000000080f17a0c5a67f663a9bc9969eb37e81666d9321125f0e293656f8a37",
        nonce: 2259603767,
        previousblockhash: "0000000027c2488e2510d1acf4369787784fa20ee084c258b58d9fbd43802b5e",
        size: 215,
        time: 1231475020,
        time_to_confirm: 132,
        tx: [
          "9962d5c704ec27243364cbe9d384808feeac1c15c35ac790dffd1e929829b271"
        ],
        txcount: 1,
        version: 1,
        vin_total: 0,
        vout_total: 50
      });
    });

    describe('Method Save', function() {
      it('should be able to save without problems', function(done) {
        return block.save(function(err) {
          should.not.exist(err);
          done();
        });
      });

      it('should be able to show an error when try to save without hash', function(done) {
        block.hash = '';

        return block.save(function(err) {
          should.exist(err);
          done();
        });
      });
    });

    afterEach(function(done) {
      Block.remove({});
      done();
    });
    after(function(done) {
      Block.remove().exec();
      done();
    });
  });
});

这是我的model.js 测试文件。我得到的错误是:

  <Unit Test>
    Model Block:
      Method Save
        1) "before each" hook

  0 passing (2s)
  1 failing

  1) <Unit Test> Model Block: "before each" hook:
     Error: timeout of 2000ms exceeded

不知道为什么需要 2 秒或什么需要 2 秒。我怎样才能知道为什么某件事要花这么长时间?

【问题讨论】:

    标签: mocha.js


    【解决方案1】:

    它告诉你什么花了这么长时间。此消息:

     <Unit Test> Model Block: "before each" hook:
    

    对应于这个beforeEach调用:

    describe('<Unit Test>', function() {
      describe('Model Block:', function() {
        beforeEach(function(done) {
          block = new Block({
          //....
    

    如果你想知道为什么要花这么长时间,你可以在new Block执行的那一行下一个断点,然后单步执行。

    【讨论】:

    • 即使我注释掉beforeEach 中的所有内容,我也会收到超时:` beforeEach(function(done) { });`
    猜你喜欢
    • 2020-10-12
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2019-10-30
    • 2020-03-26
    • 2015-09-30
    相关资源
    最近更新 更多