[英]Stub ES6 class with sinon.js
I have a controller class which instantiates a model class and I want to test that the controller uses the correct parameters when it instantiates the model.我有一个控制器类,它实例化一个模型类,我想测试控制器在实例化模型时是否使用了正确的参数。 I have found that stubbing methods on a class with sinon is no problem, but if I need to stub the constructor, I can't get it to work.我发现使用 sinon 对类进行存根方法没有问题,但是如果我需要存根构造函数,则无法使其工作。
This is my controller:这是我的控制器:
import settings from '../../config/settings';
import model from '../models/Form';
let content;
export default class Form {
constructor (app) {
content = new model(app.settings.content);
}
}
And this is the test (so far)这是测试(到目前为止)
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();
});
});
});
I run the tests with this command:我使用以下命令运行测试:
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"
The model class is built with the same pattern as the controller (eg export default class, constructor, etc).模型类使用与控制器相同的模式构建(例如导出默认类、构造函数等)。 The problem is that in the controller constructor the model is not a stub but just the class itself.问题在于,在控制器构造函数中,模型不是存根,而只是类本身。
Any suggestions on how to do this, or even whether I need to be testing this are more than welcome.关于如何做到这一点的任何建议,甚至我是否需要对此进行测试都非常受欢迎。
Here is the unit test solution using additional library proxyquire
and make an assertion for the ES6 class constructor.这是使用附加库proxyquire
并为 ES6 类构造函数进行断言的单元测试解决方案。
./controller/Form.js
: ./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
: ./models/Form.js
:
export default class FormModel {
private content;
constructor(content) {
this.content = content;
}
}
./controller/Form.test.js
: ./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));
});
});
});
Unit test result with coverage report:带有覆盖率报告的单元测试结果:
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 |
---------------|----------|----------|----------|----------|-------------------|
Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/33915599源代码: https : //github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/33915599
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.