You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First I would like to thanks in advance for any help or guidance that could be provided. I am new with rewire and I wanted to mock up a class with a constructor and several prototypes. A really trivial example of what I am trying to do, is to mock User Class in the code bellow, I would appreciate any guidance.
User Class
function User(name) {
this.name = name;
}
User.prototype.getName = function getName() {
return this.name;
};
module.exports = User;
Company Module
var user = require('user');
var users = [];
exports.setUser = function setUser(firstName, lastName) {
users.push(new User(firstName + lastName));
}
exports.getUser = function getUser(id) {
return users[id].getName();
}
Test Module
var company = rewire('./company');
// Here is were I would need to mock the User class.
var userMock = {
.... // How to set the User constructor?
.... // Prototypes follow the same pattern as exports?
getName: function getName() {
// Testing code behavior with empty results for example
return null;
}
}
company.__set__('user', userMock);
Thanks in advance for your time and support
The text was updated successfully, but these errors were encountered:
basdag
changed the title
Rewire and Classes Constructors
Rewire and Classes + Constructors + Prototypes
Feb 23, 2015
@ameykpatil There should be no limitation. You can rewire just every variable. So in this case you just need to re-define the user variable with your mock:
functionUserMock(){}// Put your prototype methods herecompany.__set__('user',UserMock);
Hello everyone,
First I would like to thanks in advance for any help or guidance that could be provided. I am new with
rewire
and I wanted to mock up a class with a constructor and several prototypes. A really trivial example of what I am trying to do, is to mockUser Class
in the code bellow, I would appreciate any guidance.Thanks in advance for your time and support
The text was updated successfully, but these errors were encountered: