Possible to stub constructors?
See original GitHub issueFor 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:
- Created 8 years ago
- Reactions:1
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
I’m having the same problem as @neilghosh How do I stub a class constructor and methods?