Sinonjs stub not replacing methodHow to replace all occurrences of a string in JavaScriptHow to replace innerHTML of a div using jQuery?Fastest method to replace all instances of a character in a stringNodeJs, Mocha and MongooseHow do I specify a Sinon.JS spy function as part of a class definition?Stubbing stripe with sinon - using stub.yieldsSinon stub a function within a functionSinon calledWith and calledWithMatch fail for objects EDIT: Sinon spy on object constructorExpress req.body not Workingwait for sinon stubbed promise to resolve before making assertion on sinon spy

Should I join an office cleaning event for free?

Factorio analysis: data munging

DOS, create pipe for stdin/stdout of command.com(or 4dos.com) in C or Batch?

Simulate Bitwise Cyclic Tag

Is there a familial term for apples and pears?

Magento2: Product backend page error

Prevent a directory in /tmp from being deleted

A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?

least quadratic residue under GRH: an EXPLICIT bound

Chess with symmetric move-square

Series about a young woman who can sense supernatural beings and works in auto repair

How old can references or sources in a thesis be?

Is this food a bread or a loaf?

Set-theoretical foundations of Mathematics with only bounded quantifiers

Circuitry of TV splitters

What is the command to reset a PC without deleting any files

Why do we use polarized capacitors?

cryptic clue: mammal sounds like relative consumer (8)

Tenured professor's husband convicted of a drugs trafficking felony - are there any career implications?

Are tax years 2016 & 2017 back taxes deductible for tax year 2018?

Shell script can be run only with sh command

How can bays and straits be determined in a procedurally generated map?

"listening to me about as much as you're listening to this pole here"

Can Medicine checks be used, with decent rolls, to completely mitigate the risk of death from ongoing damage?



Sinonjs stub not replacing method


How to replace all occurrences of a string in JavaScriptHow to replace innerHTML of a div using jQuery?Fastest method to replace all instances of a character in a stringNodeJs, Mocha and MongooseHow do I specify a Sinon.JS spy function as part of a class definition?Stubbing stripe with sinon - using stub.yieldsSinon stub a function within a functionSinon calledWith and calledWithMatch fail for objects EDIT: Sinon spy on object constructorExpress req.body not Workingwait for sinon stubbed promise to resolve before making assertion on sinon spy






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Based on an example, I am trying to stub a DB function. But I get "ReferenceError: Database is not defined". Not sure why stub not working! Please help!



Below is my code to be tested -






module.exports = 
getName : function(name)
console.log(name);
,
setupNewUser : function(info, callback)
var user =
name: info.name,
nameUpperCase: info.name.toUpperCase()
;
console.log(user.name,user.nameUpperCase)
try
Database.save(user, callback);

catch(err)
callback(err);







Below is my test -




const expect = require('chai').expect;
const sinon = require('sinon');
const crypto = require('../crypto/crypto');

describe("test crypto module",function()
it("should pass object with correct values to save",function()
console.log("save");
var save = sinon.stub(Database, 'save');

var info = name: 'test' ;
var expectedUser =
name: info.name,
nameUpperCase: info.name.toUpperCase()
;

crypto.setupNewUser(info, function() );
sinon.assert.calledWith(save, expectedUser);
save.restore();


)
)












share|improve this question






















  • Can you show where Database is defined in your code? Is it the result of a call to require or something else?

    – Phil Booth
    Mar 9 at 8:21

















0















Based on an example, I am trying to stub a DB function. But I get "ReferenceError: Database is not defined". Not sure why stub not working! Please help!



Below is my code to be tested -






module.exports = 
getName : function(name)
console.log(name);
,
setupNewUser : function(info, callback)
var user =
name: info.name,
nameUpperCase: info.name.toUpperCase()
;
console.log(user.name,user.nameUpperCase)
try
Database.save(user, callback);

catch(err)
callback(err);







Below is my test -




const expect = require('chai').expect;
const sinon = require('sinon');
const crypto = require('../crypto/crypto');

describe("test crypto module",function()
it("should pass object with correct values to save",function()
console.log("save");
var save = sinon.stub(Database, 'save');

var info = name: 'test' ;
var expectedUser =
name: info.name,
nameUpperCase: info.name.toUpperCase()
;

crypto.setupNewUser(info, function() );
sinon.assert.calledWith(save, expectedUser);
save.restore();


)
)












share|improve this question






















  • Can you show where Database is defined in your code? Is it the result of a call to require or something else?

    – Phil Booth
    Mar 9 at 8:21













0












0








0








Based on an example, I am trying to stub a DB function. But I get "ReferenceError: Database is not defined". Not sure why stub not working! Please help!



Below is my code to be tested -






module.exports = 
getName : function(name)
console.log(name);
,
setupNewUser : function(info, callback)
var user =
name: info.name,
nameUpperCase: info.name.toUpperCase()
;
console.log(user.name,user.nameUpperCase)
try
Database.save(user, callback);

catch(err)
callback(err);







Below is my test -




const expect = require('chai').expect;
const sinon = require('sinon');
const crypto = require('../crypto/crypto');

describe("test crypto module",function()
it("should pass object with correct values to save",function()
console.log("save");
var save = sinon.stub(Database, 'save');

var info = name: 'test' ;
var expectedUser =
name: info.name,
nameUpperCase: info.name.toUpperCase()
;

crypto.setupNewUser(info, function() );
sinon.assert.calledWith(save, expectedUser);
save.restore();


)
)












share|improve this question














Based on an example, I am trying to stub a DB function. But I get "ReferenceError: Database is not defined". Not sure why stub not working! Please help!



Below is my code to be tested -






module.exports = 
getName : function(name)
console.log(name);
,
setupNewUser : function(info, callback)
var user =
name: info.name,
nameUpperCase: info.name.toUpperCase()
;
console.log(user.name,user.nameUpperCase)
try
Database.save(user, callback);

catch(err)
callback(err);







Below is my test -




const expect = require('chai').expect;
const sinon = require('sinon');
const crypto = require('../crypto/crypto');

describe("test crypto module",function()
it("should pass object with correct values to save",function()
console.log("save");
var save = sinon.stub(Database, 'save');

var info = name: 'test' ;
var expectedUser =
name: info.name,
nameUpperCase: info.name.toUpperCase()
;

crypto.setupNewUser(info, function() );
sinon.assert.calledWith(save, expectedUser);
save.restore();


)
)








module.exports = 
getName : function(name)
console.log(name);
,
setupNewUser : function(info, callback)
var user =
name: info.name,
nameUpperCase: info.name.toUpperCase()
;
console.log(user.name,user.nameUpperCase)
try
Database.save(user, callback);

catch(err)
callback(err);







module.exports = 
getName : function(name)
console.log(name);
,
setupNewUser : function(info, callback)
var user =
name: info.name,
nameUpperCase: info.name.toUpperCase()
;
console.log(user.name,user.nameUpperCase)
try
Database.save(user, callback);

catch(err)
callback(err);







const expect = require('chai').expect;
const sinon = require('sinon');
const crypto = require('../crypto/crypto');

describe("test crypto module",function()
it("should pass object with correct values to save",function()
console.log("save");
var save = sinon.stub(Database, 'save');

var info = name: 'test' ;
var expectedUser =
name: info.name,
nameUpperCase: info.name.toUpperCase()
;

crypto.setupNewUser(info, function() );
sinon.assert.calledWith(save, expectedUser);
save.restore();


)
)





const expect = require('chai').expect;
const sinon = require('sinon');
const crypto = require('../crypto/crypto');

describe("test crypto module",function()
it("should pass object with correct values to save",function()
console.log("save");
var save = sinon.stub(Database, 'save');

var info = name: 'test' ;
var expectedUser =
name: info.name,
nameUpperCase: info.name.toUpperCase()
;

crypto.setupNewUser(info, function() );
sinon.assert.calledWith(save, expectedUser);
save.restore();


)
)






javascript sinon-chai






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 9 at 4:00









Saranya MohandasSaranya Mohandas

1




1












  • Can you show where Database is defined in your code? Is it the result of a call to require or something else?

    – Phil Booth
    Mar 9 at 8:21

















  • Can you show where Database is defined in your code? Is it the result of a call to require or something else?

    – Phil Booth
    Mar 9 at 8:21
















Can you show where Database is defined in your code? Is it the result of a call to require or something else?

– Phil Booth
Mar 9 at 8:21





Can you show where Database is defined in your code? Is it the result of a call to require or something else?

– Phil Booth
Mar 9 at 8:21












1 Answer
1






active

oldest

votes


















0














In order for this line of your test to run, you need to have defined Database in the test code too:



var save = sinon.stub(Database, 'save');


Your original code snippet doesn't show how Database is defined there. If it is the result of a call to require, you might just need to add an equivalent require to your test code (because of how node's module cache works):



const Database = require('../path/to/my/db/module');


Alternatively, if something more complex is going on like it's the result of a call to some function, you might need to inject it using something like proxyquire so that the function returns your stubbed copy. But you'd need to show us the definition of Database in your code to know that for certain.






share|improve this answer























  • Ok now I get it that for stubbing I need to add those dependencies as well. I was confused on that part to use dependencies in test or not. Thanks a lot! One more question if you could clarify pls? Can I define the database method something like this var Database = save : function(arg1,cb) in my test and stub it instead of adding dependency?

    – Saranya Mohandas
    Mar 9 at 16:44












  • You can do that if you're also injecting Database into the code under test somehow. But there's nothing in your example code to suggest you are doing that, so you might need to add it.

    – Phil Booth
    Mar 10 at 17:25











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073853%2fsinonjs-stub-not-replacing-method%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














In order for this line of your test to run, you need to have defined Database in the test code too:



var save = sinon.stub(Database, 'save');


Your original code snippet doesn't show how Database is defined there. If it is the result of a call to require, you might just need to add an equivalent require to your test code (because of how node's module cache works):



const Database = require('../path/to/my/db/module');


Alternatively, if something more complex is going on like it's the result of a call to some function, you might need to inject it using something like proxyquire so that the function returns your stubbed copy. But you'd need to show us the definition of Database in your code to know that for certain.






share|improve this answer























  • Ok now I get it that for stubbing I need to add those dependencies as well. I was confused on that part to use dependencies in test or not. Thanks a lot! One more question if you could clarify pls? Can I define the database method something like this var Database = save : function(arg1,cb) in my test and stub it instead of adding dependency?

    – Saranya Mohandas
    Mar 9 at 16:44












  • You can do that if you're also injecting Database into the code under test somehow. But there's nothing in your example code to suggest you are doing that, so you might need to add it.

    – Phil Booth
    Mar 10 at 17:25















0














In order for this line of your test to run, you need to have defined Database in the test code too:



var save = sinon.stub(Database, 'save');


Your original code snippet doesn't show how Database is defined there. If it is the result of a call to require, you might just need to add an equivalent require to your test code (because of how node's module cache works):



const Database = require('../path/to/my/db/module');


Alternatively, if something more complex is going on like it's the result of a call to some function, you might need to inject it using something like proxyquire so that the function returns your stubbed copy. But you'd need to show us the definition of Database in your code to know that for certain.






share|improve this answer























  • Ok now I get it that for stubbing I need to add those dependencies as well. I was confused on that part to use dependencies in test or not. Thanks a lot! One more question if you could clarify pls? Can I define the database method something like this var Database = save : function(arg1,cb) in my test and stub it instead of adding dependency?

    – Saranya Mohandas
    Mar 9 at 16:44












  • You can do that if you're also injecting Database into the code under test somehow. But there's nothing in your example code to suggest you are doing that, so you might need to add it.

    – Phil Booth
    Mar 10 at 17:25













0












0








0







In order for this line of your test to run, you need to have defined Database in the test code too:



var save = sinon.stub(Database, 'save');


Your original code snippet doesn't show how Database is defined there. If it is the result of a call to require, you might just need to add an equivalent require to your test code (because of how node's module cache works):



const Database = require('../path/to/my/db/module');


Alternatively, if something more complex is going on like it's the result of a call to some function, you might need to inject it using something like proxyquire so that the function returns your stubbed copy. But you'd need to show us the definition of Database in your code to know that for certain.






share|improve this answer













In order for this line of your test to run, you need to have defined Database in the test code too:



var save = sinon.stub(Database, 'save');


Your original code snippet doesn't show how Database is defined there. If it is the result of a call to require, you might just need to add an equivalent require to your test code (because of how node's module cache works):



const Database = require('../path/to/my/db/module');


Alternatively, if something more complex is going on like it's the result of a call to some function, you might need to inject it using something like proxyquire so that the function returns your stubbed copy. But you'd need to show us the definition of Database in your code to know that for certain.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 9 at 8:44









Phil BoothPhil Booth

4,07312433




4,07312433












  • Ok now I get it that for stubbing I need to add those dependencies as well. I was confused on that part to use dependencies in test or not. Thanks a lot! One more question if you could clarify pls? Can I define the database method something like this var Database = save : function(arg1,cb) in my test and stub it instead of adding dependency?

    – Saranya Mohandas
    Mar 9 at 16:44












  • You can do that if you're also injecting Database into the code under test somehow. But there's nothing in your example code to suggest you are doing that, so you might need to add it.

    – Phil Booth
    Mar 10 at 17:25

















  • Ok now I get it that for stubbing I need to add those dependencies as well. I was confused on that part to use dependencies in test or not. Thanks a lot! One more question if you could clarify pls? Can I define the database method something like this var Database = save : function(arg1,cb) in my test and stub it instead of adding dependency?

    – Saranya Mohandas
    Mar 9 at 16:44












  • You can do that if you're also injecting Database into the code under test somehow. But there's nothing in your example code to suggest you are doing that, so you might need to add it.

    – Phil Booth
    Mar 10 at 17:25
















Ok now I get it that for stubbing I need to add those dependencies as well. I was confused on that part to use dependencies in test or not. Thanks a lot! One more question if you could clarify pls? Can I define the database method something like this var Database = save : function(arg1,cb) in my test and stub it instead of adding dependency?

– Saranya Mohandas
Mar 9 at 16:44






Ok now I get it that for stubbing I need to add those dependencies as well. I was confused on that part to use dependencies in test or not. Thanks a lot! One more question if you could clarify pls? Can I define the database method something like this var Database = save : function(arg1,cb) in my test and stub it instead of adding dependency?

– Saranya Mohandas
Mar 9 at 16:44














You can do that if you're also injecting Database into the code under test somehow. But there's nothing in your example code to suggest you are doing that, so you might need to add it.

– Phil Booth
Mar 10 at 17:25





You can do that if you're also injecting Database into the code under test somehow. But there's nothing in your example code to suggest you are doing that, so you might need to add it.

– Phil Booth
Mar 10 at 17:25



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid …


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073853%2fsinonjs-stub-not-replacing-method%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page