1
votes

I would like to know if I am missing anything with regard to sinon.js I have tried using sinon.stub().returns and yields but am unable to get the result. Any pointers would be helpful

I have a module which calls another module that returns the value from the DB

var users = require('/users');
module.exports.getProfileImage = function (req, res) {
var profile = {};
    else {
        users.findOne("email", req.session.user.email, function (err, user) {
            if (err) {
                res.status(400).send();
            }
            else if (!user) {

                //return default image
            }
            else if (user) {
                //Do some other logic here
            }
       });

};

I am using mocha as the testing framework and am also using sinon. The problem that I am facing is when i create a stub of users.findOne to return a value the control does not come to my else if (user) condition.

my unit test case is as follows

describe("Return image of user",function(){

var validRequest = null;
validRequest={
        session:{
            user:{
                email:'[email protected]',
                role:'Hiring Company'
            }
        }
    };
 it("Should return an image from the file if the user is present in db",function(done){
var findOneUserResponse ={
        companyName:"xyz",
        email:"[email protected]"
    };
    var findOne = sinon.stub(mongoose.Model, "findOne");
    findOne.callsArgWith(1,null,findOneUserResponse);

    user.getProfileImage(validRequest,response);

    var actualImage = response._getData();
    findOne.restore();
    done();

};

};
1

1 Answers

0
votes

So I went through the sinon.js documentation http://sinonjs.org/docs/ and came across what I was missing

describe("Return image of user",function(){

var validRequest = null;
validRequest={
    session:{
        user:{
            email:'[email protected]',
            role:'Hiring Company'
        }
    }
};
it("Should return an image from the file if the user is present in db",function(done){
var findOneUserResponse ={
    companyName:"xyz",
    email:"[email protected]"
};
var findOne = sinon.stub(mongoose.Model, "findOne",function(err,callback){
   callback(null,findOneUserResponse);
)};

user.getProfileImage(validRequest,response);

var actualImage = response._getData();
findOne.restore();
done();
};
};