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.
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.
./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));
});
});
});
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.