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

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme