使用sinon.js存根ES6类

时间:2015-11-25 11:40:59

标签: node.js unit-testing sinon

我有一个实例化模型类的控制器类,我想测试控制器在实例化模型时使用正确的参数。我发现使用sinon的类的存根方法没有问题,但如果我需要存根构造函数,我就无法使它工作。

这是我的控制者:

import settings from '../../config/settings';
import model from '../models/Form';

let content;

export default class Form {

  constructor (app) {
    content = new model(app.settings.content);
  }
}

这是测试(到目前为止)

import {assert} from 'chai';
import sinon from 'sinon';
import fs from 'fs';
import settings from '../../../config/settings';
import model from '../../../lib/models/Form';
import controller from '../../../lib/controllers/Form';

let mocks;

describe('Form controller', () => {

  beforeEach((done) => {
    mocks = {};
    mocks.model = sinon.createStubInstance(model);
    done();
  });

  afterEach((done) => {
    mocks = null;
    done();
  });

  describe('New Forms controller', () => {

    beforeEach((done) => {
      mocks.app = {
        settings: {
          content: '/content/path/',
          views: '/views/path/'
        }
      };
      mocks.controller = new controller(mocks.app);
      done();
    });

    it('Instantiates a model', (done) => {
      assert.isTrue(mocks.model.calledWith(mocks.app.settings.content));
      done();
    });
  });
});

我使用此命令运行测试:

npm run test-unit
//which equates to
"test-unit": "BABEL_DISABLE_CACHE=1 ./node_modules/.bin/mocha --recursive --check-leaks --reporter spec --bail --compilers js:babel/register ./test/unit"

使用与控制器相同的模式构建模型类(例如,导出默认类,构造函数等)。问题是在控制器构造函数中,模型不是存根,而只是类本身。

有关如何执行此操作的任何建议,或者甚至是否需要对此进行测试都非常受欢迎。

1 个答案:

答案 0 :(得分:0)

这是使用附加库proxyquire并为ES6类构造函数声明的单元测试解决方案。

./controller/Form.js

import model from "../models/Form";

let content;

export default class Form {
  constructor(app) {
    content = new model(app.settings.content);
  }
}

./models/Form.js

export default class FormModel {
  private content;
  constructor(content) {
    this.content = content;
  }
}

./controller/Form.test.js

import { assert } from "chai";
import sinon from "sinon";
import proxyquire from "proxyquire";

let mocks = {};

describe("Form controller", () => {
  describe("New Forms controller", () => {
    beforeEach(() => {
      mocks.app = {
        settings: {
          content: "/content/path/",
          views: "/views/path/",
        },
      };
      mocks.model = sinon.stub();
      const { default: controller } = proxyquire("./Form", {
        "../models/Form": {
          default: mocks.model,
        },
      });
      mocks.controller = new controller(mocks.app);
    });

    afterEach(() => {
      mocks = null;
    });

    it("Instantiates a model", () => {
      assert.isTrue(mocks.model.calledWith(mocks.app.settings.content));
    });
  });
});

带有覆盖率报告的单元测试结果:

  Form controller
    New Forms controller
      ✓ Instantiates a model


  1 passing (200ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |    95.45 |      100 |    88.89 |    95.45 |                   |
 controller    |      100 |      100 |      100 |      100 |                   |
  Form.test.ts |      100 |      100 |      100 |      100 |                   |
  Form.ts      |      100 |      100 |      100 |      100 |                   |
 models        |    66.67 |      100 |       50 |    66.67 |                   |
  Form.ts      |    66.67 |      100 |       50 |    66.67 |                 4 |
---------------|----------|----------|----------|----------|-------------------|

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