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;
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();
)
)
javascript sinon-chai
add a comment |
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();
)
)
javascript sinon-chai
Can you show whereDatabase
is defined in your code? Is it the result of a call torequire
or something else?
– Phil Booth
Mar 9 at 8:21
add a comment |
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();
)
)
javascript sinon-chai
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
javascript sinon-chai
asked Mar 9 at 4:00
Saranya MohandasSaranya Mohandas
1
1
Can you show whereDatabase
is defined in your code? Is it the result of a call torequire
or something else?
– Phil Booth
Mar 9 at 8:21
add a comment |
Can you show whereDatabase
is defined in your code? Is it the result of a call torequire
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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 injectingDatabase
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 injectingDatabase
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
add a comment |
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.
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 injectingDatabase
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
add a comment |
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.
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.
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 injectingDatabase
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
add a comment |
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 injectingDatabase
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Can you show where
Database
is defined in your code? Is it the result of a call torequire
or something else?– Phil Booth
Mar 9 at 8:21