admin管理员组文章数量:1332394
I have a class under test that creates instances of another class. I want to stub out the entirety of the second class, so that its constructor never gets called. For example, If I have this setup:
Test.js
class Test {
constructor() {
}
func() {
let foo = new Foo()
foo.hello()
}
}
Foo.js
class Foo {
constructor() {
this.a = 1
this.b = 2
this.c = 3
console.log('original constructor')
}
hello() {
console.log('original hello')
}
goodbye() {
console.log('original goodbye')
}
}
In my test file, I want to somehow stub out the entirety of the Foo
class, so that when I run my test for for Test.func()
it doesn't call the original Foo
constructor, but rather a stubbed constructor that returns an fake Foo
object. I'll then stub the hello
function of the fake Foo
object to print stubbed hello
instead of original hello
.
How can I stub the entire class like this?
NOTE: I do NOT want to create a stub instance that I can use inside my test file. I need to stub the constructor itself, so that if something up the stack calls the constructor, it gets back a stub instance.
I have a class under test that creates instances of another class. I want to stub out the entirety of the second class, so that its constructor never gets called. For example, If I have this setup:
Test.js
class Test {
constructor() {
}
func() {
let foo = new Foo()
foo.hello()
}
}
Foo.js
class Foo {
constructor() {
this.a = 1
this.b = 2
this.c = 3
console.log('original constructor')
}
hello() {
console.log('original hello')
}
goodbye() {
console.log('original goodbye')
}
}
In my test file, I want to somehow stub out the entirety of the Foo
class, so that when I run my test for for Test.func()
it doesn't call the original Foo
constructor, but rather a stubbed constructor that returns an fake Foo
object. I'll then stub the hello
function of the fake Foo
object to print stubbed hello
instead of original hello
.
How can I stub the entire class like this?
NOTE: I do NOT want to create a stub instance that I can use inside my test file. I need to stub the constructor itself, so that if something up the stack calls the constructor, it gets back a stub instance.
Share edited Dec 18, 2017 at 20:16 ewok asked Dec 18, 2017 at 18:03 ewokewok 21.5k56 gold badges163 silver badges266 bronze badges2 Answers
Reset to default 5In sinon documentation:
If you want to create a stub object of MyConstructor, but don’t want the constructor to be invoked, use this utility function.
var stub = sinon.createStubInstance(MyConstructor)
http://sinonjs/releases/v1.17.7/stubs/
// A.spec.js
import { A } from './A';
import * as BClass from './B';
describe('A Test', () => {
beforeEach(() => {
class MockB { // The fake B
constructor(params) { /* do some things */ }
introduce() { /* return a stub */ }
locate() { /* do some things and return a stub */ }
}
sinon.stub(BClass, 'B').callsFake((args) => {
return new MockB(args);
}
}
it('should assert personhood', () => { /* bla bla */ }
});
The below link helped me a lot.
https://medium./@kirien.eyma/mocking-imported-class-dependencies-in-sinon-js-with-typescript-8854f9c00ee
本文标签: javascriptsinon How to stub an entire classrather than just a methodStack Overflow
版权声明:本文标题:javascript - sinon: How to stub an entire class, rather than just a method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742261485a2442596.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论