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 badges
Add a ment  | 

2 Answers 2

Reset to default 5

In 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