Janani Janani - 5 months ago 113
Node.js Question

How can I unit test my facbook login using passport js in mocha nodejs?

I am new to unit testing. I have been writing test case using mocha for Nodejs. In my case need to write test case for facebook login. I have firstly tried with facebook-mock but i was not able to complete the task.This is my test case where I have used zombie,

var chai = require('chai');
var assert = chai.assert;
var server;
var Browser = require('zombie');
describe("login using social sites",function () {
this.timeout(40000);
beforeEach(function () {
server = require('../../../server').server;
browser = new Browser({ site: 'http://localhost:3000' });
});
it("should login with facebook",function (done) {

browser.visit('/auth/facebook',function (err,brw) {

if(err){
throw err;
}

assert.equal(brw.location.pathname, '/auth/facebook/callback');

done();

});



});


afterEach(function () {
server.close();
});

});


And the server.js file is,

var express = require('./config/express');

var app = express();

var server = app.listen(3000, function () {
var port = server.address().port;
console.log('Server running at %s', port);
});

module.exports = {
app : app,
server : server
};


This is the error which i have got after executing the unit test,

1) login using social sites should login with facebook:
Uncaught TypeError: connect ECONNREFUSED 127.0.0.1:3000
at G:\Janani\Tasks\CCSProject\node_modules\zombie\lib\pipeline.js:89:15
at tryCatcher (G:\Janani\Tasks\CCSProject\node_modules\zombie\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (G:\Janani\Tasks\CCSProject\node_modules\zombie\node_modules\bluebird\js\release\promise.js:504:31)
at Promise._settlePromise (G:\Janani\Tasks\CCSProject\node_modules\zombie\node_modules\bluebird\js\release\promise.js:561:18)
at Promise._settlePromise0 (G:\Janani\Tasks\CCSProject\node_modules\zombie\node_modules\bluebird\js\release\promise.js:606:10)
at Promise._settlePromises (G:\Janani\Tasks\CCSProject\node_modules\zombie\node_modules\bluebird\js\release\promise.js:681:18)
at Async._drainQueue (G:\Janani\Tasks\CCSProject\node_modules\zombie\node_modules\bluebird\js\release\async.js:138:16)
at Async._drainQueues (G:\Janani\Tasks\CCSProject\node_modules\zombie\node_modules\bluebird\js\release\async.js:148:10)
at Immediate.Async.drainQueues [as _onImmediate] (G:\Janani\Tasks\CCSProject\node_modules\zombie\node_modules\bluebird\js\release\async.js:17:14)


Please anyone guide me to fix this issue. And if any other specific framework if available please guide me with that. Thanks in Advance!!

Answer

I got fixed with this issue!!!

describe("login using social sites",function () {

    this.timeout(40000);


    beforeEach(function () {
        server = require('../../../server').server;

        // browser = new Browser({ site: 'http://localhost:3000' });



    });


    it("should login with facebook",function (done) {

        Browser.visit('http://127.0.0.1:3000/auth/facebook',function (err,brw) {

            if(err){
                throw err;
            }

            brw.fill('email','aaa@gmail.com').fill('pass', 'password')
                .pressButton('login', function (err,brow) {
                    brw.assert.success();
                    done();
                });

        });



    });


    afterEach(function () {
        server.close();
    });

});
Comments