question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible to stub constructors?

See original GitHub issue

For example-

In my ‘email.js’ I have:

var mandrill = require('mandrill-api/mandrill');
var mandrill_client = new mandrill.Mandrill('API_KEY_HERE');

exports.sendTemplate = function(templateName, to, variables, callback) {

    console.log('mandrill_client === ', mandrill_client)

    var message = {
        //omitted for brevity
    };

    mandrill_client.messages.sendTemplate({
        template_name: templateName,
        template_content: [],
        message: message,
    }, function(result) {
        //... do something
        return callback();
    }, function(err) {
        //...
    });
}

Then, in my test, I want to stub out mandrill, so that when a new mandrill_client is created, it gets my stubbed version, ultimately with a sinon.spy on mandrill_client.messages.sendTemplate

//not sure what mandrillStub should be??

email = proxyquire('../../../app/helpers/email', {
    'mandrill-api/mandrill': mandrillStub
});

Is this possible?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

2reactions
bendruckercommented, May 15, 2015

Sure – there’s nothing special here. Proxyquire is changing what require itself returns. You’re not mutating shared values and so you specifically can stub constructors and other primitives that you cannot mutate through a shared reference between your source and test code.

Here’s a rough start to get you going:

function MockMandrill (key) {
  this.messages = {
    sendTemplate: sinon.stub()
  }
}

var mandrillStub = {Mandrill: MockMandrill}
1reaction
phrozencommented, Dec 31, 2019

I’m having the same problem as @neilghosh How do I stub a class constructor and methods?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript: Mocking Constructor using Sinon
MyWidget. I was able to successfully mock/stub it in the global scope. Now my issue is inside the object that calls the MyWidget...
Read more >
Stubbing constructor does not work · Issue #1892 · sinonjs ...
You're stubbing a reference to the constructor ( prototype.constructor ). It's not possible to stub the constructor itself due to language ...
Read more >
Mock Java Constructors With Mockito | Configuration and ...
The new method that makes mocking object constructions possible is Mockito.mockConstruction() . This method takes a non-abstract Java class that ...
Read more >
How to mock/stub a constructor
I want to know, how can I mock the constructor function of an class? ... var mock = sinon.mock(MyObject.prototype); ... Is it possible?...
Read more >
Not Calling the Original Constructor - Mockery
In some cases this is not a desired behavior, as the constructor might issue calls to other methods, or other object collaborators, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found