【发布时间】:2018-02-23 08:41:14
【问题描述】:
我正在尝试对一些依赖于我制作的某些类的快速中间件进行单元测试。
中间件.js
const MyClass = require('../../lib/MyClass');
const myClassInstance = new MyClass();
function someMiddleware(req, res) {
myClassInstance.get().then(function(resp) {
res.render('somefile.html');
});
};
Test.js
const MyClass = require('../../lib/MyClass');
const sinon = require('sinon');
const chai = require('chai');
const expect = chai.expect;
// File we are testing
const someMiddleware = require('path/to/middleware');
MyClassMock = sinon.spy(function() {
return sinon.createStubInstance(MyClassMock);
});
describe('My Middleware', function() {
let renderSpy, classStub;
let res = {
render: function() {}
}
beforeEach(function() {
renderSpy = sinon.stub(res, 'render');
})
afterEach(function() {
renderSpy.restore();
})
it('should call render function', function() {
someMiddleware.someMiddleware(req, res);
expect(renderSpy.calledOnce);
});
});
我已经尝试了很多东西,但是我似乎并不能真正模拟这个类并模拟创建的实例!当它运行时,它会尝试运行包含相关依赖项的实际类。
各位干杯!
【问题讨论】:
-
这里有很多混乱 :-) 你有很多代码似乎是为你的情况设置的,但实际上并没有用于 任何东西 (例如:
MyClassMock、MyClass、...)。也许是编辑留下的?
标签: javascript unit-testing express sinon chai