我正在尝试使用proxyquire测试一个对象。该对象是一个带有一些依赖项的简单JavaScript / TypeScript类。我正在使用proxyquire和sinon来覆盖这些依赖关系,并且工作得很好。但是我不能创建类的新实例来在测试程序上运行它的方法。
我试图在返回新实例的类上创建一个静态方法,但错误是"Proxy.create不是一个函数“
// a stub of one of the dependents in
const poolRepositoryStub = {
insert: _.constant({}),
'@noCallThru': true,
};
const Proxy = proxyquire('../../dist/src/components/proxy.js', {
'../repository/pool-repository': poolRepositoryStub,
'@noCallThru': true,
});
describe('Test ReportCheckComponent', () => {
before(() => {
// this one failed say Proxy is not a constructor
new Proxy()
// and this one also failed on Proxy.create is not a function
Proxy.create();
});
it('report-check.spec.ts: test run method using sinon stub', () => {
sinon
.stub(poolRepositoryStub, 'insert')
.returns(Promise.resolve(null))
// ... the rest of the test code
});
});发布于 2019-04-01 22:34:38
我测试的对象是用expert的typescript编写的,如下所示:
export default class MyClass { ... }将其更改为:
export = MyClass;解决问题
https://stackoverflow.com/questions/55438874
复制相似问题