Here is my simple program where I'm trying to get user's data via ldapClient. I need to test it without internet connection so wondering how to mock that result.on events to return appropriate data.
var request = require('request');
var ldap = require('ldapjs');
....
var ldapClient = ldap.createClient(ldapConfig);
....
var MY_CLASS = {
getData: function (userId, cb) {
if (!ldapConfig) {
return cb(new Error('ldap is not configured'));
}
ldapClient.search('xxxx.com', { ldapConfig },
function (err, result) {
if (err) {
return cb(err);
}
result.on('searchEntry', function (entry) {
if (entry) {
return entry;
}
});
result.on('error', function (err) {
cb(err);
});
result.on('end', function () {
cb(null, 'END');
});
});
}
};
module.exports = MY_CLASS;
Looking for something (see below) but assume I need to use a spy. But how do I define it in that deep nested class?
before(()=>{
sinon
.stub(MY_CLASS.ldapClient, 'search')
.yields(???);
});
after(()=>{
MY_CLASS.ldapClient.search.restore();
});
If you are stubbing the library, and not exporting from the original class file, you will need to import instead of referencing it as a method/property on the class
Then you'll want to use callsArg from sinon to call the callback
var ldapClient = ldap.createClient(ldapConfig);
...
var ldapStub;
before(()=>{
ldapStub = sinon
.stub(ldapClient, 'search')
.callsArg(2);
});
after(()=>{
ldapStub.restore();
});
You can then include assertions on the stub (for example ldapStub.calledOnce should be true, etc)
Related
I'm new to node.js, and I'm trying to return a value from a function, but for some reason it's not working as planned.
In the code below, I want to return AuthToken from within the Try statement, but instead, it returns the values from outside the Try statement. If I delete that return line, then it just returns undefined. If I add return AuthToken instead then it also returns undefined - obviously because it hasn't completed the Try statement...
I've checked out a bunch of similar problems, but they haven't answered my problem.
Any suggestions?
"use strict";
const request = require('request'); // node package to create api request
var AuthToken;
/*
some stuff
*/
module.exports = {
FUNC1: (param1) => {
/*DO STUFF */
},
FUNC2: (param2) => {
/*DO STUFF*/
},
GetAuthToken: () => {
var options = {
/*OPTIONS*/
};
try {
request(options, (err, resp, body) => {
if (!err) {
let data = JSON.parse(body);
AuthToken = data["accessToken"];
return AuthToken; // <= I expect the AuthToken to be returned here.
} else {
console.log(err)
}
})
} catch (err) {
console.log(err)
}
return "Here instead"; //<= But the function is returning here instead before it's performed the request...
},
}
I eventually figured out that the request function is an asynchronous and so needs to be called with an async function.
I took the example code from here and modified it for my purposes.
I'm using nodejs exif library to retrieve metadata from JPEG files.
this lib is used this way :
import * as exif from 'exif'
new exif.ExifImage('path_to_file.jpg', function(err, metadata){ ... })
I've found everywhere how to stub a class method using sinon, pretty simple.
But I don't get how to stub this class constructor so that metadata (or err if I want to test failing case) will be the stubbed value I need to perform my test.
We can still use Sinon with callsFake function. Here is the example:
// src.js
const exif = require("exif");
function readImage() {
// convert to promise for easier testing
return new Promise((resolve, reject) => {
new exif.ExifImage("path_to_file.jpg", function(err, metadata) {
if (err) {
reject(err);
}
resolve(metadata);
});
});
}
module.exports = { readImage };
meanwhile for test
// test.js
const sinon = require('sinon');
const src = require('./src');
const exif = require('exif');
const expect = require('chai').expect;
describe('test exifimage', () => {
let exifStub;
beforeEach(function() {
exifStub = sinon.stub(exif, 'ExifImage')
})
afterEach(function() {
sinon.restore();
})
it('test when success', async () => {
const metadata = 'cinta';
// mock ExifImage as similar as its function signature
exifStub.callsFake((filename, callback) => {
callback(null, metadata); // error is set as null then we set metadata
});
const response = await src.readImage();
expect(response).to.equal(metadata);
});
it('test when error', async () => {
const errorMessage = 'this is error';
exifStub.callsFake((filename, callback) => {
callback(errorMessage); // specify error, error is defined as first param
});
try {
await src.readImage();
} catch (error) {
expect(error).to.equal(errorMessage);
}
});
});
Hope it helps
I'm getting undefined when I trying to print a value to console which is returned by an asynchronous function.
This is happening in my controller function (dumyController.js) which calls a function written in helper (DBHelper.js), for DRY approach, which
asynchronously fetches data from a model function in DBHelperModel.js
dumyController.js
var dbHelpers = require('../helpers/helpers');
exports.dumyControllerFunc = function (req, res) {
var result= dbHelpers.dumyHelperFunc(165);
console.log(result);
};
DBHelper.js
var dbHelp = require('../models/DBHelperModel');
module.exports = {
dumyHelperFunc: function (userId) {
dbHelp.fetchDataFromDB(userId, function (err, rows) {
var res;
if (err) {
return null;
}
else {
res.send(rows.member_code);
}
});
}
};
DBHelperModel.js
var db = require('../db');
var DBHelpers = {
fetchDataFromDB: function (userId, callback) {
var query = `SELECT member_code FROM members where id=?`;
db.query(query, userId, callback);
},
};
module.exports = DBHelpers;
db.js
var mysql = require('mysql');
var connection = mysql.createPool({
host: '127.0.01',
user: 'root',
password: '',
database: 'dumyDB'
});
module.exports = connection;
I know I'm not getting value because of the asynchronous nature of the function but can anyone tell me how to fetch the value with an architecture like given above. I'm new to nodeJS. Thanks!
I think you want a solution about the asynchronous in nodejs.
The promise,that is a universal and popular solution
Use a callback,That is a approach in nodejs's way,anyway,that is native nodejs's asynchronous solution .
Other asysn: Generate and async, that's es6 feature,and the latter is support by nodejs7+
The promise is most recommend,it has a good looks structure if has a asycn chain, The callback looks bad on that.
This is probably what you trying to achieve:
https://repl.it/MA0Z/0
var DBHelpers = {
fetchDataFromDB: function (userId, callback) {
callback(null, 'hello fetchDataFromDB');
},
};
var dbhelp = {
dumyHelperFunc: function (userId, callback) {
DBHelpers.fetchDataFromDB(userId, function (err, rows) {
if (err) {
callback('error bro');
} else {
callback(null, rows);
}
});
}
}
dbhelp.dumyHelperFunc(165, function (error, result) {
console.log(result);
});
I have written below code in one file:
models/exported.js
module.exports = {
processedList: function(store_name) {
var t;
var tradeIds = exported.find({storename: store_name}, function (err, value) {
if (err) return console.error(err);
return value;
}).select('tid -_id');
}, // Export connection here
};
I have another file in routes
routes/exported.js
var exported = require('../models/exported.js');
var tradeIds = exported.processedList(storename);
console.log('simer'+tradeIds);
}
but I get undefined in console.log. If instead of return statement in processedlist I write console.log then the result gets console. But my requirement is to return data from model file to route file.
I am new to express and node js.
I guidance would be highly appreciated.
Acoording to your question, you want calling a function from route and get return response from your function to route. simple use callback functions.
models/exported.js
module.exports = {
processedList: function (store_name, callback) {
var t;
var tradeIds = exported.find({storename: store_name}, function (err, value) {
if (err) {
callback("error", err)
} else {
callback("success", value)
}
}).select('tid -_id');
}
}
routes/exported.js
var exported = require('../models/exported.js');
exported.processedList('storename', function (err, results) {
if (err == 'error') {
console.log(err);
} else {
console.log(results);
}
});
You are trying sync operation in async environment. processedList may or may not have completed when you try to console log tradeIds. NodeJS would not wait for it to complete because it is asynchronous in nature (by design and it is not a bug). You can pass callback rather than executing this way.
models/exported.js
module.exports = {
processedList: function(store_name, cb) {
var t;
var tradeIds = exported.find({storename: store_name}, function (err, value) {
if (err) return cb(err);
cb(null, value);
}).select('tid -_id');
}, // Export connection here
};
routes/exported.js
var exported = require('../models/exported.js');
exported.processedList(storename, function(err, results) {
if (err) { console.log(err); }
console.log(results);
});
This makes sure that console.log happens only when processedList finishes execution.
Having some problems unit testing the below code, I'm unsure whether it is possible or not due to the way it is coded.
storeModel.js
var storeSchema = new Schema({
storeId : { type: String, index: true},
storeName : String
});
var model = mongoose.model('store', storeSchema);
var findStoresById = function(ids, callback) {
model.find({ storeId: { $in: ids }}, function (err, result) {
if (err) callback(err);
callback(err, result);
});
};
return {
findStoresById: findStoresById,
schema: storeSchema,
model: model
};}();
Which i test like so..
it('will call "findStoresById" and return matched values [storeId: 1111] ', function (done) {
storeModel.findStoresById(['1111'], function(err, store) {
assert.equal(store[0].storeId, '1111');
assert.equal(store[0].storeName, 'StoreName');
assert.equal(err, null);
done();
});
});
However the problem when i implement the following code within a separate function:
get: function (req, res) {
if (req.query.storeIds) {
var ids = req.query.storeIds.split(',');
storeModel.findStoresById(ids, function(err, stores) {
if (err) {
return res.send(err);
}
if (_.isEmpty(stores)) {
var error = {
message: "No Results",
errorKey: "XXXX"
}
return res.status(404).json(error);
}
return res.json(stores);
}); ...
How can i unit test this, i dont want to mock it as there is functionality in the "findStoreById" that needs testing, or is a refactor needed? suggestions?
I'd contest that you actually should be stubbing findStoreById because in not doing so get cannot strictly be unit tested, in that it's not isolated and could fail through no fault of its own. Seeing as the functionality you'd want to test lies within the callback of findStoreById and not the method itself, we can happily stub it and use the yields method of sinon to invoke its callback accordingly.
Note that, if you're testing routes, it's preferable to use supertest else you'll have a lot of mocking of request and response methods on your hands. Therefore, for example:
var request = require('supertest');
var express = require('express');
// stub database method
sinon.stub(storeModel, 'findStoresById');
// create a test app/route to which we direct test requests
var app = express();
app.get('/', myRouteFunction);
it('sends a 404 error when no stores are found', function(done) {
// use the second argument of `yields` to pass a result to the callback
storeModel.findStoresById.yields(null, []);
request(app).get('/').expect(404, done);
});
it('responds with any stores found', function(done) {
// pass an array of found stores to the callback
storeModel.findStoresById.yields(null, [{_id: 1}]);
request(app).get('/').end(function(err, res) {
if(err) return done(err);
assert.deepEqual(res.body, [{_id: 1}]);
done();
});
});
If what you want is test static's and method's of certain Mongoose model, I would recommend you to use sinon and sinon-mongoose.
But first, some tips for your code
var storeSchema = new Schema({
storeId : { type: String, index: true},
storeName : String
});
// 1) If you will callback with the same 'err' and 'result', pass the callback directly
function findStoresById(ids, callback) {
// Instead of this...
this.find({ storeId: { $in: ids } }, function (err, result) {
if (err) callback(err);
callback(err, result);
});
// Use this... it's the same, but shorter
this.find({ storeId: { $in: ids } }, callback);
}
// 2) Declare a static method on your model, instead of export manually (see Monggose documentation for more info)
storeSchema.static('findStoresById', function (ids, callback) {
});
// 3) Create your model after the statics were declared, and use CamelCase
var model = mongoose.model('Store', storeSchema);
// 4) Export just your model
// If you want the model -> var Store = mongoose.model('Store')
// If you want the schema -> var schema = Store.schema
// If you want to call your method -> Store.findStoresById(...)
module.exports = model;
Then, to test the method findStoresById
var sinon = require('sinon');
require('sinon-mongoose');
var Store = mongoose.model('Store');
sinon.mock(Store)
.expects('find').withArgs({ storeId: { $in: ['id1', 'id2'] } })
.yields(null, 'SUCCESS!');
Store.findStoresById(['id1', 'id2'], function (err, res) {
assert(res, 'SUCCESS!');
});
You can find working (and simple) examples on the sinon-mongoose repo.