如何模拟getPass函数的响应

时间:2018-04-16 13:51:58

标签: javascript node.js mocha sinon chai

您好我正在编写我的javascript函数的单元测试用例。我正在使用摩卡,柴,期待,sinon

app.js

module.exports = {
    saveInGlobal: async () => {
      try {
        if (global.pass !== null && global.pass !== '') {
          return module.exports.getpass().then((res) => {
            return global.pass = res;
          });
        }
      } catch (err) {

      }
    },
    getPass: async () => {
       return "test";
    } 
}

我想模拟getPass()函数,然后断言global.pass。任何人都可以建议如何使用getPass()

模仿sinon

2 个答案:

答案 0 :(得分:0)

基本功能由sinon.stub()提供,它覆盖了对象中的某些属性 - 在您的情况下为module.getPass

const theModule = require('theModule'); // module with getPass
it('foo', () => {
    // Prepare
    const getPassStub = sinon.stub(theModule, 'getPass');
    getPassStub.resolves('mocked');
    // Act

    // call your code here

    // Assert
    expect(getPassStub.calledOnce).to.be.true;
    getPassStub.restore()
});

这样可行,但仅限于您的测试通过。如果失败,存根存活(restore未被调用)并且getPass仍然被模拟以进行其余的测试执行。

考虑使用:after(() => module.getPass.restore())或更优选使用sinon.Sandbox

存根文档:http://sinonjs.org/releases/v4.5.0/stubs/

Sandbox doc:http://sinonjs.org/releases/v4.5.0/sandbox/

答案 1 :(得分:0)

您不能将try/catch与异步代码(promise.then)一起使用。所以我将其删除。这是单元测试:

app.js

module.exports = {
  saveInGlobal: async () => {
    if (global.pass !== null && global.pass !== "") {
      return module.exports.getPass().then((res) => {
        return (global.pass = res);
      });
    }
  },
  getPass: async () => {
    return "test";
  },
};

app.test.js

const app = require("./app");
const sinon = require("sinon");
const { expect } = require("chai");

describe("49858991", () => {
  afterEach(() => {
    sinon.restore();
  });
  describe("#saveInGlobal", () => {
    it("should save in global", async () => {
      global.pass = "a";
      const getPassStub = sinon.stub(app, "getPass").resolves("mock value");
      await app.saveInGlobal();
      sinon.assert.calledOnce(getPassStub);
      expect(global.pass).to.be.eq("mock value");
    });

    it("should do nothing", async () => {
      global.pass = null;
      const getPassStub = sinon.stub(app, "getPass").resolves("mock value");
      await app.saveInGlobal();
      sinon.assert.notCalled(getPassStub);
      expect(global.pass).to.be.eq(null);
    });
  });

  describe("#getPass", () => {
    it('should return "test"', async () => {
      const actual = await app.getPass();
      expect(actual).to.be.eq("test");
    });
  });
});

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

 49858991
    #saveInGlobal
      ✓ should save in global
      ✓ should do nothing
    #getPass
      ✓ should return "test"


  3 passing (16ms)

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

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