带有sinon.js的存根ES6类

问题描述 投票:1回答:1

我有一个实例化模型类的控制器类,我想测试控制器在实例化模型时是否使用了正确的参数。我发现使用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"

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

非常欢迎您提出任何有关如何执行此操作的建议,甚至我是否需要对此进行测试。

javascript node.js unit-testing sinon
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.