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

Stubbing function from same module under test not replacing real implementation #325

Closed
danruziska opened this issue Jan 17, 2018 · 4 comments

Comments

@danruziska
Copy link

Hi guys, I'd like some help witha simple thing I'm missing.

I'm testing a function from a module which has a dependency from another function of the same module.
So I wanted to stub the dependency function:

let contentClient = require('../../content-client');

      var getAggregatedContentDetail = td.replace(contentClient, 'getAggregatedContentDetail');
      td.when(getAggregatedContentDetail(td.matchers.anything(), td.matchers.anything(), td.matchers.anything(), td.matchers.anything()))
      .thenReturn('1234');

//Function under test
let contentDetails = contentClient.getSimpleContentDetails();

In short: getSimpleContentDetails calls getAggregatedContentDetail internally, but executing the test, it's getting the real implementation of getAggregatedContentDetail, not the stub.

What I'm missing?

Thanks

@searls
Copy link
Member

searls commented Jan 17, 2018

As far as I can tell, the above should work. The only thing I could ask at this point is you provide a minimal reproducible example repository for us to check into

@leandro-toledo-uux
Copy link

leandro-toledo-uux commented Jan 18, 2018

@searls , just created the requested repository

@Schoonology
Copy link
Contributor

@leandro-toledo-uux In the case of the sample repo, the issue is that getDetails calls getId directly, rather than through the exported object (i.e. module.exports.getId). There is no way for testdouble to replace getId itself; instead, it has to replace the reference to getId via the exported object.

@searls
Copy link
Member

searls commented Jan 18, 2018

Additionally, this is only an issue because your example is contaminating the veracity of the subject under test by taking the module being tested and faking out one portion of it. This is a more severe sub-type of a partial mock and I'd encourage you to break the relationship up so it's abundantly clear to the reader what's real and what's fake.

@searls searls closed this as completed Jan 18, 2018
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

4 participants