尝试使用mocha编写此代码的测试用例,但不确定如何做

时间:2019-06-25 06:35:52

标签: node.js mocha async.js

编写了一个非常简单的代码,将异步用于3个作业。我希望使用Mocha来测试此代码。任何人都可以提供指针来开始使用此代码吗?

var async = require('async');
var each = require('async-each');
var arr = [{name:'job1', delay:100}, {name: 'job2', delay:200},{name:'job3', delay:300}];
async.eachLimit(arr, 3, function(job, callback) {
    console.log("Execute the job: " + job.name);
    setTimeout(function() {
        callback(null, job.name);
    }, job.delay);

}, function(error){
    console.log(error);
});

1 个答案:

答案 0 :(得分:0)

首先,您应该知道,我们只需要测试我们的应用程序代码,而不是async库的代码。因为async库已经过全面测试。以下是我们的应用程序代码中的功能:

function(job, callback) {
    console.log("Execute the job: " + job.name);
    setTimeout(function() {
        callback(null, job.name);
    }, job.delay);
}

function(error){
    console.log(error);
}

因此,我们需要进行一些重构,以便于测试。这是完整的工作示例:

index.js

const async = require("async");
const arr = [
  { name: "job1", delay: 100 },
  { name: "job2", delay: 200 },
  { name: "job3", delay: 300 },
];

function main() {
  async.eachLimit(arr, 3, iteratee, function(error) {
    console.log(error);
  });
}

function iteratee(job, callback) {
  console.log(`Execute the job: ` + job.name);
  setTimeout(function() {
    callback(null, job.name);
  }, job.delay);
}

module.exports = {
  main,
  iteratee,
};

index.spec.js

const { main, iteratee } = require("./");
const sinon = require("sinon");
const async = require("async");

describe("56748106", () => {
  let clock;
  beforeEach(() => {
    clock = sinon.useFakeTimers();
  });
  afterEach(() => {
    clock.restore();
    sinon.restore();
  });
  describe("iteratee", () => {
    it("should pass", () => {
      sinon.spy(global, "setTimeout");
      const job = { name: "job1", delay: 2000 };
      const callback = sinon.stub();
      iteratee(job, callback);
      clock.tick(2000);
      sinon.assert.calledWithExactly(callback, null, job.name);
      sinon.assert.calledWithExactly(setTimeout, sinon.match.func, job.delay);
    });
  });

  describe("main", () => {
    it("should handle error", () => {
      const logSpy = sinon.spy(console, "log");
      const mError = new Error("some error");
      sinon.stub(async, "eachLimit").yieldsRight(mError);
      main();
      sinon.assert.calledWithExactly(
        async.eachLimit,
        [
          { name: "job1", delay: 100 },
          { name: "job2", delay: 200 },
          { name: "job3", delay: 300 },
        ],
        3,
        iteratee,
        sinon.match.func,
      );
      sinon.assert.calledWithExactly(logSpy, mError);
    });
  });
});

覆盖率100%的单元测试结果:

 56748106
    iteratee
Execute the job: job1
      ✓ should pass
    main
Error: some error
    at Context.it (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/src/stackoverflow/56748106/index.spec.js:1:4680)
    at callFn (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runnable.js:387:21)
    at Test.Runnable.run (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runnable.js:379:7)
    at Runner.runTest (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:535:10)
    at /Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:653:12
    at next (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:447:14)
    at /Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:457:7
    at next (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:362:14)
    at Immediate._onImmediate (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:425:5)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
      ✓ should handle error


  2 passing (20ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |      100 |      100 |      100 |      100 |                   |
 index.js      |      100 |      100 |      100 |      100 |                   |
 index.spec.js |      100 |      100 |      100 |      100 |                   |
---------------|----------|----------|----------|----------|-------------------|

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/56748106

相关问题