Please help me? I have been wrecking my mind but I can not find out how should I stub a variable? Am I wrong? Should I use Spy?
How should I test this code
module.exports = async () => {
var variable = 'something';
var taskProcessor = require('taskprocessor');
try {
taskProcessor(variable).then().catch();
//blah blah
//blah blah
//blah blah
//blah blah
} catch (error) {
console.log(err);
}
};
First of all, you should know what is stub or spy (I excluded mocks intentionally)
We use doubles to Control a method's behavior, then change the test direction to cover all paths in our test.
The spy wraps around the function, it does not replace its functionality. But with stub we can define the output: Spy is literally sending an spy inside your enemies (in this case your code :D) to mimic the behavior of a genuine entity and gather information for you!
now let's go back to your question!
You can use rewire module in this case. From it's git page
rewire adds a special setter and getter to modules so you can modify their behavior for better unit testing. You may
- inject mocks for other modules or globals like process
- inspect private variables
- override variables within the module.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.