使用sinon来破坏ES6超级方法

Rah*_*uly 6 tdd mocha.js sinon ecmascript-6

我在使用Sinon存根基类方法时遇到了问题.在下面的示例中,我将对基类方法GetMyDetails的调用存根如下.我相信有更好的方法.

actor = sinon.stub(student.__proto__.__proto__,"GetMyDetails");
Run Code Online (Sandbox Code Playgroud)

而且这个价值也是最重要的.

我在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;
Run Code Online (Sandbox Code Playgroud)

现在我有一个扩展了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);
    });
}

}
Run Code Online (Sandbox Code Playgroud)

我试图使用mocha为此创建一个测试用例

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

let request;
let response;
let actor;


beforeEach(function () {
    request = {
        session: {
            user: {
                email: 'student@student.com',
                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();
});
});
Run Code Online (Sandbox Code Playgroud)

Est*_*ask 8

原型方法应该直接在原型上进行存根/间谍:

sinon.stub(Actor.prototype,"GetMyDetails");
Run Code Online (Sandbox Code Playgroud)