簡體   English   中英

使用 sinon.js 存根 ES6 類

[英]Stub ES6 class with sinon.js

我有一個控制器類,它實例化一個模型類,我想測試控制器在實例化模型時是否使用了正確的參數。 我發現使用 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"

模型類使用與控制器相同的模式構建(例如導出默認類、構造函數等)。 問題在於,在控制器構造函數中,模型不是存根,而只是類本身。

關於如何做到這一點的任何建議,甚至我是否需要對此進行測試都非常受歡迎。

這是使用附加庫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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM