I am trying to test async.waterfall by stubbing one of my functions using Sinon.js.
// functions.js
module.exports = {
// function I don't want to run
doBigThing: function() {
console.log("[doBigThing] was called");
},
// function I want to stub
myFunction: function(number, callback) {
console.log("[myFunction] was called");
doBigThing();
callback(null, number);
},
// function I want to test
waterfall: function(callback) {
return async.waterfall([
async.constant(5), // 5 just for the demo
myFunction
], callback);
}
}
And my test is:
describe('water', function() {
it ('successfully falls', function() {
// function under test
var waterfall = functions.waterfall;
var callback = function(err, number) {
expect(err).to.be.null;
expect(number).to.equal(5);
};
// I would like this stub to run instead of functions.myFunction
sinon.stub(functions, 'myFunction', function(number, callback) {
console.log("[myFunction] stub was called");
callback(null, number);
});
waterfall(callback);
// I suppose this is happening: myFunction(5, callback)
expect(functions.myFunction.withArgs(5, callback)).to.have.been.called;
expect(callback).to.have.been.called;
});
});
So the test passes, but the stub is ignored, because doBigThing was called:
Water
✓ successfully falls
[myFunction] was called
[doBigThing] was called
Instead I would like to see
Water
✓ successfully falls
[myFunction] stub was called
I am probably missing out on something and I would appreciate your help.
You're stubbing functions object's method myFunction, but in waterfall method you're calling a myFunction function (I actually can't run your code in my environment, I get "ReferenceError: myFunction is not defined"). So this should work:
// functions.js
var functions = {
// function I don't want to run
doBigThing: function() {
console.log("[doBigThing] was called");
},
// function I want to stub
myFunction: function(number, callback) {
console.log("[myFunction] was called");
functions.doBigThing(); // CHANGE HERE
callback(null, number);
},
// function I want to test
waterfall: function(callback) {
return async.waterfall([
async.constant(5), // 5 just for the demo
functions.myFunction // CHANGE HERE
], callback);
}
};
module.exports = functions;
Related
SO, I have 2 files, a.js and b.js
in a.js, i have a test(); function that I'm calling from b.js
I also have the following code to help me find in what line of b.js is calling the function, and that works fine. But I also want to be getting the filename, and the code below gives me a.js instead of b.js.. any pointers?
Object.defineProperty(global, "__stack", {
get() {
const orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack) {
return stack;
};
const err = new Error;
Error.captureStackTrace(err, arguments.callee);
const stack = err.stack;
Error.prepareStackTrace = orig;
return stack;
},
});
Object.defineProperty(global, "__line", {
get() {
return __stack[1].getLineNumber();
},
});
Object.defineProperty(global, "__filename", {
get() {
return __stack[1].getFileName();
},
});
OK So, I'm not sure why but the
Object.defineProperty(global, "__filename", {
get() {
return __stack[1].getFileName();
},
});
part of the code provided wasn't working like expected. Idk what it was doing, all I know is I tried the following and it's working fine.
Object.defineProperty(global, "__callerInfo", {
get() {
return [__stack[1].getLineNumber(), __stack[1].getFileName()];
},
});
I'm just saving the line number in one constant and the file name in a different one
Following the Intern user guide, I wrote a simple page object:
define(function(require) {
function ListPage(remote) {
this.remote = remote;
}
ListPage.prototype = {
constructor: ListPage,
doSomething: function(value) {
return this.remote
.get(require.toUrl('http://localhost:5000/index.html'))
.findByCssSelector("[data-tag-test-id='element-of-interest']")
.click().end();
}
};
return ListPage;
});
In the test, I want to call doSomething twice in a row, like this:
define(function(require) {
var registerSuite = require('intern!object');
var ListPage = require('../support/pages/ListPage');
registerSuite(function() {
var listPage;
return {
name: 'test suite name',
setup: function() {
listPage = new ListPage(this.remote);
},
beforeEach: function() {
return listPage
.doSomething('Value 1')
.doSomething('Value 2');
},
'test function': function() {
// ...
}
};
});
});
However, when I run the test, I get this error:
TypeError: listPage.doSomething(...).doSomething is not a function
I tried some approaches described in this question, to no avail.
A better way to implement page objects with Intern is as helper functions rather than Command wrappers. Groups of related helper functions can then be used to create Page Object modules.
// A helper function can take config parameters and returns a function
// that will be used as a Command chain `then` callback.
function doSomething(value) {
return function () {
return this.parent
.findByCssSelector('whatever')
.click()
}
}
// ...
registerSuite(function () {
name: 'test suite',
'test function': function () {
return this.remote.get('page')
// In a Command chain, a call to the helper is the argument
// to a `then`
.then(doSomething('value 1'))
.then(doSomething('value 2'));
}
}
I am having trouble figuring out how to pass the objects method rather than sort "generic prototype" method when doing callback.
function Client() {
this.name = "hello";
}
Client.prototype.apiCall = function(method, params, callback) {
callback();
}
Client.prototype.onLogin = function(error, data) {
console.log(this.name);// undefined!!!!
}
Client.prototype.start = function() {
var self = this;
self.apiCall('rtm.start', {
}, self.onLogin) // passing of method like this does not work.
}
I am passing the onLogin method but well it does not work. This is code I have re-written. Previously I nested all methods inside the Client function but well, I learned that that is not the way to do it so now I am trying using prototype.
I know there is some solution "binding" the onLogin function inside the Client() function but well I want to understand the issue.
You need to bind the apiCalls context to the callback using bind:
Client.prototype.apiCall = function(method, params, callback) {
var bound = callback.bind(this);
bound();
}
Otherwise, the this within onLogin is set to the global object.
See Call, Apply And Bind for further details.
Basically .bind(obj) returns a function which, when called, will internally use (obj) as this.
So you create this bound and then you call it.
You can use call or apply to bind this, see snippet. I've modified your code for demonstration purposes. Hope it clarifies things for you
function Client() {
this.name = "hello";
}
Client.prototype = {
apiCall: function(method, params, callback) {
try {
var trial = method.call(this, params);
callback.apply(this, [null, trial]);
} catch (e) {
callback.apply(this, [e, null]);
}
},
onLogin: function(error, data) {
if (error) {
Helpers.report('<b style="color: red">' +
'An error occured!</b> <i>' +
error.message + '</i>')
} else {
Helpers.report(this.name, ' (data.result = ' + data.result + ')');
}
},
start: function() {
Helpers.useCSS(1);
// error from this.rtm.start
Helpers.report('Command: <code>', 'this.apiCall(this.rtm.start, {will: \'not work\'}, this.onLogin);','</code>');
this.apiCall(this.rtm.start, {will: 'not work'}, this.onLogin);
// this.rtm.works is ok
Helpers.report('<br>Command: <code>',
'this.apiCall(this.rtm.works, {will: \'work\'}, this.onLogin);',
'</code>');
this.apiCall(this.rtm.works, {
will: 'work'
}, this.onLogin);
},
// --------------------------------
// added rtm for snippet demo
rtm: {
start: function(params) {
return anerror;
},
works: function(params) {
return {
result: 'worked, <code>params: ' + JSON.stringify(params) + '</code>'
};
}
},
};
new Client().start(); //<= here
<script src="https://rawgit.com/KooiInc/Helpers/master/Helpers.js"></script>
I'm testing router and have two functions, and I need to test if first function was called and second was not. There is method toHaveBeenCalled but there is no method to test if function was not called. How can I test that?
I have code like this:
var args, controller, router;
beforeEach(function() {
controller = {
foo: function(name, id) {
args = [].slice.call(arguments);
},
bar: function(name) {
}
};
spyOn(controller, "foo").and.callThrough();
spyOn(controller, "bar").and.callThrough();
router = new route();
router.match('/foo/bar/{{id}}--{{name}}', controller.foo);
router.match('/foo/baz/{{id}}--{{name}}', controller.bar);
router.exec('/foo/bar/10--hello');
});
it('foo route shuld be called', function() {
expect(controller.foo).toHaveBeenCalled();
});
it('bar route shoud not be called', function() {
// how to test if bar was not called?
});
Use the not operator:
expect(controller.bar).not.toHaveBeenCalled();
I have this js class which has an anonymous function to retrieve some query results. Since the function which handles the results is anonymous I can't save the results in a this.var variable and use them somewhere else since this in the anonymous function references to the window object. I can't return it as a function return either so how do I do with those results to have them available somewhere else?
someObject = {
// this.db is created, no need to paste that code
dbGetAnimals: function () {
this.db.readTransaction(function(tx) {
tx.executeSql("SELECT * FROM animals", function(tx, results){
return results;
} )
});
},
printAllAnimals: function () {
var animals = this.dbGetAnimals();
alert (animals);// undefined
}
}
someObject.printAllAnimals();
You can create a local variable to hold your this reference. Your anonymous function will become a closure, so it will be able to see that local var.
dbGetAnimals: function () {
var myself = this;
this.db.readTransaction(function(tx) {
tx.executeSql("SELECT * FROM animals", function(tx, results){
myself.var = results;
} )
});
},
You're trying to do traditional synchronous/non-blocking programming where you want to do asynchronous programming.
var someObject = function()({
this.dbGetAnimals = function (callback) {
db.readTransaction(function(tx) {
tx.executeSql("SELECT * FROM animals", function(tx, results){
callback(results);
} )
});
},
this.printAllAnimals = function (callback) {
this.dbGetAnimals(callback);
}
})();
someObject.printAllAnimals(function(animals) {
alert(animals);
}
There are much cleaner ways to do this, but for async programming you have to learn to do everything on callbacks instead of direct returns.
This is happening because the anonymous function in executeSql is a callback function which is not executed until the query compmetes, which by definition will be after dbGetAnimals returns. That's why your call to dbGetAnimals is returning undefined.
You'll have to pass a callback function to receive the query resutls from within the executSql callback:
someObject = {
// this.db is created, no need to paste that code
dbGetAnimals: function (callback) {
this.db.readTransaction(function(tx) {
tx.executeSql("SELECT * FROM animals", function(tx, results){
callback(results);
} );
});
},
printAllAnimals: function () {
this.dbGetAnimals(function(animals) {
alert(animals)
});
}
}
someObject.printAllAnimals();