Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faking internal calls #243

Closed
danruziska opened this issue May 9, 2017 · 1 comment
Closed

Faking internal calls #243

danruziska opened this issue May 9, 2017 · 1 comment

Comments

@danruziska
Copy link

danruziska commented May 9, 2017

Hi,

I'd like to know how to mock internal calls in this specific scenario:

module1.js

function function1() {
  //Some logic code
  return module2.function2(arg1);
}

I want to fake function2, but the code under test, which is function1, runs the real implementation.

Right now, I'm stuck with this solution:

const module1 = require('path/to/module1.js');

it('should return fake function2', () => {
  //Arrange
  let function2Return = {
    test:'test',
    test2:'test2'
  };
  //module2.js contains function2()
  let mod2= testdouble.replace('path/to/module2.js');
  testdouble.when(mod2.function2().thenReturn(
    function2Return
  )); 
  //Act
  //module1.js contains function1()
  let result = module1.function1();
  //Assert
  assert.strictEqual('test2',result.test2);
});

How do I tie the real implementation of function1() with the fake implementation of function2()?

Thanks

@searls
Copy link
Member

searls commented May 9, 2017

You need to require module1 after you've replaced module2.

it('should return fake function2', () => {
  //Arrange
  let function2Return = {
    test:'test',
    test2:'test2'
  };
  //module2.js contains function2()
  let mod2= testdouble.replace('path/to/module2.js');
  const module1 = require('path/to/module1.js'); // <-- like this
  testdouble.when(mod2.function2().thenReturn(
    function2Return
  )); 
  //Act
  //module1.js contains function1()
  let result = module1.function1();
  //Assert
  assert.strictEqual('test2',result.test2);
});

@searls searls closed this as completed May 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants