Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stubbing ES6 prototype methods using sinon

I am having a problem stubbing the prototype methods of the super class using Sinon. In the example below I am stubbing the call to super class method GetMyDetails as follows. I am sure there is a better way.

actor = sinon.stub(student.__proto__.__proto__,"GetMyDetails");

And also the value of this.Role ends up being undefined.

I have created a simple class in javascript

"use strict";
class Actor {
constructor(userName, role) {
    this.UserName = userName;
    this.Role = role;
}

GetMyDetails(query,projection,populate,callback) {
    let dal = dalFactory.createDAL(this.Role);
    dal.PromiseFindOneWithProjectionAndPopulate(query, projection, populate).then(function (data) {
        callback(null,data);
    }).catch(function (error) {
        routesLogger.logError(this.Role, "GetMyDetails", error);
        return callback(error);
    })

}
}
 module.exports = Actor;

Now I have a child class that extends Actor.js

"use strict";
 class student extends Actor{
constructor(username, role) {
    super(username, role);
    this.UserName = username;       
    this.Role = role;
}

GetMyDetails(callback) {
    let query = {'username': this.UserName};
    let projection = {};
    let populateQuery = {}

    super.GetMyDetails(query, projection, populateQuery, function (err, result) {
        if (err) {
            routesLogger.logError(this.Role, "GetMyDetails", err);
            callback(err, null);
        }
        else
            callback(null, result);
    });
}

}

I have tried to create a test case for this using mocha

describe("Test Suite For Getting My Details",function(){

let request;
let response;
let actor;


beforeEach(function () {
    request = {
        session: {
            user: {
                email: '[email protected]',
                role: 'student'
            }
        },
        originalUrl:'/apssdc'
    };
    response = httpMocks.createResponse();

});

afterEach(function () {

});



it("Should get details of the student",function(done){
    let username = "student";
    let role = "Student";
    let student = new Student(username,role);
    actor = sinon.stub(student.__proto__.__proto__,"GetMyDetails");
    actor.yields(new Error(), null);

    sc.GetMyDetails(function(err,data){
        console.log(data);
        console.log(err);
    });
    done();
});
});
like image 334
Rahul Ganguly Avatar asked Dec 12 '16 12:12

Rahul Ganguly


People also ask

What is stub in Sinon?

Test stubs are functions (spies) with pre-programmed behavior. They support the full test spy API in addition to methods which can be used to alter the stub's behavior. As spies, stubs can be either anonymous, or wrap existing functions.

What is stub method in JavaScript?

What are Stubs? A test stub is a function or object that replaces the actual behavior of a module with a fixed response. The stub can only return the fixed response it was programmed to return.

How do you mock a function in Sinon?

You must call mock() on an object. To complete the test, you must call the verify() function to check that all the mock's expectations were met.

How do you spy a function in Sinon?

var spy = sinon. spy(); Creates an anonymous function that records arguments, this value, exceptions and return values for all calls.


1 Answers

Prototype methods should be stubbed/spied directly on the prototype:

sinon.stub(Actor.prototype,"GetMyDetails");
like image 101
Estus Flask Avatar answered Sep 22 '22 22:09

Estus Flask