繁体   English   中英

如何对 node-cron 作业进行单元测试

[英]How to unit test node-cron job

我需要使用 node-cron 调用一个函数,并想为此编写一个单元测试用例。 单元测试用例应该能够测试函数是否正在根据模式进行调用。

下面是我的代码

const server = (module.exports = {
  cronJob: null,
  scheduledJob: function(pattern) {
    server.cronJob = cron.schedule(pattern, () => {
      server.run();
    });
  },
  run: function() {
    console.log("run called");
  },
}); 
describe("entry point test suite", () => {
  it("should call function every second", (done) => {
    const pattern = "* * * * * *";
    let spy = sinon.spy(server, "run");
    server.scheduledJob(pattern);
    server.cronJob.Start();
    // to do wait for 3 sencond
    server.cronJob.Stop();
    expect(spy.callCount).eq(3);
  });
}); 

两个问题:

  1. 除了setTimeout什么选项,我必须等待 3 秒,以便 cron 作业将按照模式每秒运行 3 次。

  2. 此测试失败,错误为 server.cronjob.start is not a function。

我怎样才能使这项工作?

这是单元测试解决方案:

server.js

const cron = require("node-cron");

const server = (module.exports = {
  cronJob: null,
  scheduledJob: function(pattern) {
    server.cronJob = cron.schedule(pattern, () => {
      server.run();
    });
  },
  run: function() {
    console.log("run called");
  },
});

server.test.js

const server = require("./server");
const sinon = require("sinon");
const cron = require("node-cron");
const { expect } = require("chai");

describe("57208090", () => {
  afterEach(() => {
    sinon.restore();
  });
  describe("#scheduledJob", () => {
    it("should schedule job", () => {
      const pattern = "* * * * * *";
      const runStub = sinon.stub(server, "run");
      const scheduleStub = sinon
        .stub(cron, "schedule")
        .yields()
        .returns({});
      server.scheduledJob(pattern);
      sinon.assert.calledWith(scheduleStub, pattern, sinon.match.func);
      sinon.assert.calledOnce(runStub);
      expect(server.cronJob).to.be.eql({});
    });
  });

  describe("#run", () => {
    it("should run server", () => {
      const logSpy = sinon.spy(console, "log");
      server.run();
      sinon.assert.calledWith(logSpy, "run called");
    });
  });
});

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

  57208090
    #scheduledJob
      ✓ should schedule job
    #run
run called
      ✓ should run server


  2 passing (12ms)

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

您要求进行单元测试。 如果您需要集成测试,请创建一个新帖子。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM