I have my source file for which i have written test cases for
var debug = require('debug')('kc-feed:source:fb');
var request = require('request');
var config = require('../../config').root;
exports.source = function fetchFeed (callback) {
var params = {
headers: {'content-type' : 'application/jsons'},
url: config.host + "v1/social/fb/sync_feed",
method: 'GET'
};
request(params, function(err, body, response) {
if(err) {
callback(err);
}
else {
var raw_data = JSON.parse(response);
callback(null, raw_data);
}
});
};
This is my mocha test case
var chai = require('chai'),
expect = chai.expect,
app = require('../app'),
rewire = require('rewire'),
fbfeed = rewire('../src/feed/source/fb_feed'),
supertest = require('supertest'),
sinon = require('sinon'),
nock = require('nock');
describe('test cases for fb feed file', function() {
var callback;
beforeEach(function(){
callback = sinon.spy();
});
after(function(){
nock.cleanAll();
});
it('callback should be called with the response', function(done) {
nock('http://localhost:3000/')
.get('/v1/social/fb/sync_feed')
.reply(200, {});
callback = sinon.spy();
fbfeed.source(callback);
sinon.assert.calledOnce(callback);
done();
});
it('callback should be called with the error', function(done) {
nock('http://localhost:3000/')
.get('/v1/social/fb/sync_feed')
.replyWithError(new Error('err'));
fbfeed.source(callback);
sinon.assert.calledOnce(callback);
done();
});
Both my test cases fail as it says that callback is called 0 times. But the callback is always called. Please help.
It looks like the requests are still being performed asynchronously (I can't definitively say if this is expected when using nock), so you can't use spies like that.
Provide a regular callback instead:
fbfeed.source(function(err, raw_data) {
...make your assertions...
done();
});
Related
I am new to nodejs.
How do I stub my return result which is a callback.
I know that I should not access the DB when doing testing.
I am doing unit testing at the controller level.
Here is my flow on how I going to do my test based on my understanding from java.
Mock Request and Response.
Set param of request.
Mock bookDAO.selectBook so that it return a user defined result. Thus not calling DB.
Verify / assert the value of the return results. (i.e. Response must be 200, JSON format, must have column BOOK_ID, BOOK_TITLE, etc)
However, i was not able to successfully mock my function. After running npm test, this is the error that I am receiving.
2018-10-02T10:00:17.809 1) Book service
1. should list a SINGLE Book /book/id GET:
Error: selectBook cannot yield to '[object Object]' since no callback was passed. Received [XCV1234, function (result) {
res.status(200).json({
message: format(message.DEFAULT_MSG, "GET", constant.MODULE_URL),
result: result
});
}]
at throwYieldError (node_modules\sinon\lib\sinon\call.js:22:11)
at Object.yieldToOn (node_modules\sinon\lib\sinon\call.js:167:13)
at Object.yieldTo (node_modules\sinon\lib\sinon\call.js:156:31)
at Function.spyApi.(anonymous function) [as yieldTo] (node_modules\sinon\lib\sinon\spy.js:416:61)
at Context.it (test\controller\BookController.spec.js:47:17)
Am i doing it the right way? how do i return the callback result ?
bookController.js:
exports.getBook = (req, res) => {
//get from request
const id = req.params.id;
const params = [id];
bookDao.selectBook(params, function (result) {
res.status(200).json({
message: format(message.DEFAULT_MSG, "GET", constant.MODULE_URL),
result: result
});
});
};
bookDao.js:
function selectBook(params, callback) {
pool.open(connString, function (err, conn) {
conn.queryResult(query.SQL_SELECT, params, function (err, result) {
if (err) {
console.error(err);
return conn.closeSync();
}
var data = result.fetchAllSync();
// only when successful then call closeSync
result.closeSync();
return callback(data);
});
conn.close();
});
}
bookRest.js:
module.exports = (app) => {
// map to controller
const controller = require('../controller/bookController');
app.route(constant.MODULE_URL + '/:id').get(controller.getbook);
app.route(constant.MODULE_URL).put(controller.updateBooks);
};
bookController.spec.js:
process.env.NODE_ENV = 'test';
const sinon = require('sinon');
const chai = require('chai');
const chaiHttp = require('chai-http');
const should = chai.should();
const httpMocks = require('node-mocks-http');
let server = require('../../../main.js');
const bookController = require('../../../controller/bookController.js');
const bookDao = require('../../../dao/bookDao.js');
chai.use(chaiHttp);
let req = httpMocks.createRequest();
let res = httpMocks.createResponse();
describe('Book service', () => {
beforeEach(() => {
});
afterEach(() => {
});
it('1. should list a SINGLE Book /book/id GET', (done) => {
req.params.id = "XCV1234";
const selectbook = sinon.stub(bookDao, "selectbook");
bookController.getbook(req, res);
selectbook.yieldTo({BOOK_ID : "XCV1234"});
res.should.have.status(200);
res.should.be.json;
res.body.should.be.a('object');
res.body.result[0].should.include.keys(
'BOOK_ID'
);
sinon.restore();
done();
});
});
I'm afraid yieldsTo is not the appropriate method to use for this case. Based on documentation, this method is intended to target callback that passed as property as in
sinon.stub(jQuery, "ajax").yieldsTo("success", [1, 2, 3]);
jQuery.ajax({
success: function (data) {
assertEquals([1, 2, 3], data);
}
});
To solve your problem, we can use yields so it will be like:
...
// should be stubbed before `getbook` is called
sinon.stub(bookDao, "selectbook").yields({
BOOK_ID: "XCV1234"
});
bookController.getbook(req, res);
res.should.have.status(200);
...
Hope it helps
I wish doing a test for ARI ! but i cant have a request for connection with asterisk . whene i run my file with node its ok , but if i run it with mocha no result !
let chai = require('chai');
let request = require('request');
let uuid = require('uuid');
let should = chai.should();
let ari = require('ari-client');
let expect = chai.expect;
let sinon = require('sinon');
describe('Tests originationDevice ', function () {
it(" should make a call ", function (done) {
ari.connect("http://192.168.0.62:8088", "username", "password")
.then(function (ari) {
console.log('its okey');
})
.catch(function (err) {
console.log('errrror');
});
done();
});
});
Well, its just one of solution but strongly recommend to mock external api calls-
try-
it(" should make a call ", function (done) {
ari.connect("http://192.168.0.62:8088", "username", "password")
.then(function (ari) {
console.log('its okey');
res.should.true; // something like that.
done(); // calling done function
})
.catch(function (err) {
console.log('errrror');
// assert something
done(); // call done function
});
});
I am using rewire to test my node controllers. I have the following endpoint that uses request to GET some data.
exports.myGetEndpoint = function(req, res) {
return request.get({
url: 'http://baseURL/api/myGetEndpoint',
headers: {
authorization: { //etc }
},
json: true
})
.then(function(data) {
res.status(200).json(data.objects);
});
};
I want to test that when I call the get method from the controller, that request gets called with the correct arguments, but I'm lost on how to either 'stub' or 'spy' on request.
var Promise = require('bluebird');
var rewire = require('rewire');
var controller = rewire('../../controllers/myGetEndpoint.js');
var request = {
get: sinon.stub()
};
// Use rewire to mock dependencies
controller.__set__({
request: request
});
describe('myGetEndpoint', function() {
var json;
var req;
var res;
beforeEach(function(done) {
json = sinon.spy();
req = { headers: {} };
res = {
status: sinon.stub().returns({
json: json
})
};
controller.myGetEndpoint(req, res).then(function() {
done();
});
});
it('should call the correct request arguments', function() {
// lost
});
});
I am going to modify a bit your source code to use just a callback instead of a promise. I need to investigate how to stub a promise with sinon but you can get an idea on how it can be tested with a simple callback:
exports.myGetEndpoint = function(req, res) {
return request.get({
url: 'http://baseURL/api/myGetEndpoint',
headers: {
authorization: { //etc }
},
json: true
}, function (error, response, body) {
res.status(200).json(response.objects);
});
};
You should not call the controller in the beforeEach but you must call it inside the test case. The beforeEach normally is used to do the initializations before the test case:
var expect = require('chai').expect;
var Promise = require('bluebird');
var rewire = require('rewire');
var controller = rewire('../../controllers/myGetEndpoint.js');
var request = {
get: sinon.stub()
};
// Use rewire to mock dependencies
controller.__set__({
request: request
});
describe('myGetEndpoint', function() {
var status;
var json;
var req;
var res;
beforeEach(function(done) {
json = sinon.spy();
status = sinon.stub();
req = { headers: {} };
res = {
status: status.returns({
json: json
})
};
});
it('should call the correct response arguments', function(done) {
var response = {objects: ['objects', 'objects']}
request.get.yields(null, response);
controller.myGetEndpoint(req, res);
expect(status.calledWith(200)).to.equal(true);
expect(json.calledWith(response.objects)).to.equal(true);
});
});
P.S.: I didn't actually try to run it, so maybe there are some typos.
Is there a way to test those kind of middleware in express:
module.exports = function logMatchingUrls(pattern) {
return function (req, res, next) {
if (pattern.test(req.url)) {
console.log('request url', req.url);
req.didSomething = true;
}
next();
}
}
The only middleware testing i found was:
module.exports = function(request, response, next) {
/*
* Do something to REQUEST or RESPONSE
**/
if (!request.didSomething) {
console.log("dsdsd");
request.didSomething = true;
next();
} else {
// Something went wrong, throw and error
var error = new Error();
error.message = 'Error doing what this does'
next(error);
}
};
describe('Middleware test', function(){
context('Valid arguments are passed', function() {
beforeEach(function(done) {
/*
* before each test, reset the REQUEST and RESPONSE variables
* to be send into the middle ware
**/
requests = httpMocks.createRequest({
method: 'GET',
url: '/css/main.css',
query: {
myid: '312'
}
});
responses = httpMocks.createResponse();
done(); // call done so that the next test can run
});
it('does something', function(done) {
/*
* Middleware expects to be passed 3 arguments: request, response, and next.
* We are going to be manually passing REQUEST and RESPONSE into the middleware
* and create an function callback for next in which we run our tests
**/
middleware(responses, responses, function next(error) {
/*
* Usually, we do not pass anything into next except for errors, so because
* in this test we are passing valid data in REQUEST we should not get an
* error to be passed in.
**/
if (error) { throw new Error('Expected not to receive an error'); }
// Other Tests Against request and response
if (!responses.didSomething) { throw new Error('Expected something to be done'); }
done(); // call done so we can run the next test
}); // close middleware
}); // close it
}); // close context
}); // close describe
This work well with the simple middleware (it like testing basic function with callback) provided above but with more complex middleware i cannot get it work. Is it possible to test this kind of middleware?
Here's a simple setup that you could use, using chai and sinon:
var expect = require('chai').expect;
var sinon = require('sinon');
var middleware = function logMatchingUrls(pattern) {
return function (req, res, next) {
if (pattern.test(req.url)) {
console.log('request url', req.url);
req.didSomething = true;
}
next();
}
}
describe('my middleware', function() {
describe('request handler creation', function() {
var mw;
beforeEach(function() {
mw = middleware(/./);
});
it('should return a function()', function() {
expect(mw).to.be.a.Function;
});
it('should accept three arguments', function() {
expect(mw.length).to.equal(3);
});
});
describe('request handler calling', function() {
it('should call next() once', function() {
var mw = middleware(/./);
var nextSpy = sinon.spy();
mw({}, {}, nextSpy);
expect(nextSpy.calledOnce).to.be.true;
});
});
describe('pattern testing', function() {
...
});
});
From there, you can add more elaborate tests for the pattern matching, etc. Since you're only using req.url, you don't have to mock an entire Request object (as created by Express) and you can just use a simple object with a url property.
I used node-mocks-http to unit test my middleware. Here's my code:
function responseMiddleware(req, res, next) {
res.sendResponse = (...args) => {
//<==== Code removed from here
};
next();
}
And in my spec file I did it like this:
var expect = require('chai').expect;
var sinon = require('sinon');
var responseMiddleware = require('./response');
var httpMocks = require('node-mocks-http');
describe('request handler calling', function() {
it('should call next() once', function() {
var nextSpy = sinon.spy();
responseMiddleware({}, {}, nextSpy);
expect(nextSpy.calledOnce).to.be.true;
});
it('should add sendResponse key', function() {
var nextSpy = sinon.spy();
var req = httpMocks.createRequest();
var res = httpMocks.createResponse();
responseMiddleware(req, res, nextSpy);
expect(nextSpy.calledOnce).to.be.true;
responseMiddleware(req, res, () => {
expect(res).to.have.property('sendResponse');
})
});
});
If you are using async calls then you can use await and then call done() after that.
I ran into an issue today where I had passing tests that should have failed. There were some incorrectly stubbed methods using Sinon that failed to stub out the callback correctly. I would have expected execution to fail, but it passed the test, however it stopped execution early because my test messages quit logging to the console at the incorrectly stubbed method invocation.
The code I had was similar to the following:
exports.myFunc = function (req, res) {
var innerFunc = function(callback) {
fs.exists('some/dir', function (exists) {
if (!exists) { // handle error }
else {
logger.log('this prints to the console');
utilModule.stubbedMethod('some/dir', callback);
}
});
};
logger.log('this also prints to the console');
innerFunc(function (err, value) {
logger.log('this does not print to the console');
if (err) {
logger.log('this does not print to the console');
res.status(500).send(err);
} else {
logger.log('this does not print to the console, either');
res.send('success');
}
});
});
// This is the tests, now:
var app = require('../app'),
mod = require('../lib/myModule'),
request = require('supertest'),
chai = require('chai'),
sinon = require('sinon');
describe('For module myModule', function () {
...
describe('#myFunc', function () {
var fs = require('fs'),
util = require('../lib/utilModule');
describe('Happy path', function () {
var expectedResult = "expected";
before(function() {
sinon.stub(fs, 'exists').yields(true, null);
sinon.stub(mod, 'stubbedMethod').yield("some value");
// the above is wrong and should have been yield(null, null, "some value")
});
after(function () { fs.exists.restore(); mod.stubbedMethod.restore(); });
it('Replies with the right value and status 200', function () {
request(app).get('/url').expect(200, 'success').end(function(err, res) {
if (err) throw err;
}
});
});
});
Why did this test pass, and is there some way to prevent it from passing?