Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript和sinon如何监视构造函数中调用的方法?_Javascript_Node.js_Sinon - Fatal编程技术网

使用JavaScript和sinon如何监视构造函数中调用的方法?

使用JavaScript和sinon如何监视构造函数中调用的方法?,javascript,node.js,sinon,Javascript,Node.js,Sinon,我真的需要以下代码的帮助-这不是从我的程序中粘贴出来的,而是从我的头顶上粘贴下来的,但我认为它清楚地说明了问题(我相信它是完全准确的)。当我请求“spy.called”的值时,它会忽略构造函数中的调用。我如何编写代码,使构造函数内的调用由spy注册 如果不可能,我应该采取什么方法?示例代码非常感谢-非常感谢-整天都在用这个来敲我的头 function MyClass() { var self = this; this.myFunc = function() { console.l

我真的需要以下代码的帮助-这不是从我的程序中粘贴出来的,而是从我的头顶上粘贴下来的,但我认为它清楚地说明了问题(我相信它是完全准确的)。当我请求“spy.called”的值时,它会忽略构造函数中的调用。我如何编写代码,使构造函数内的调用由spy注册

如果不可能,我应该采取什么方法?示例代码非常感谢-非常感谢-整天都在用这个来敲我的头

function MyClass() {
  var self = this;
  this.myFunc = function() {
    console.log("hi");
  }
  function init() {
    self.myFunc();
  }
  init();
}


var spy = sinon.spy(new MyClass(), "myFunc");
console.log(spy.called);  // true if the spy was called at least once
// ABOVE OUTPUTS FALSE - IT FAILS TO REGISTER THE CALL IN THE CONSTRUCTOR!
spy.myFunc();
console.log(spy.called);
// ABOVE OUTPUTS TRUE AS EXPECTED

我认为你应该重新设计一下你的课程。您可以在构造函数参数中接受myFunc(仅当从使用角度来看有意义时),也可以在MyClass的原型上设置它:

function MyClass() {
    function init() {
        this.myFunc();
    }
    init();
}

MyClass.prototype.myFunc = function() {
    console.log("hi");
}

var spy = sinon.spy(MyClass.prototype, "myFunc");
new MyClass();
console.log(spy.called);

我认为你应该重新设计一下你的课程。您可以在构造函数参数中接受myFunc(仅当从使用角度来看有意义时),也可以在MyClass的原型上设置它:

function MyClass() {
    function init() {
        this.myFunc();
    }
    init();
}

MyClass.prototype.myFunc = function() {
    console.log("hi");
}

var spy = sinon.spy(MyClass.prototype, "myFunc");
new MyClass();
console.log(spy.called);

这里的问题是,当调用方法
myFunc
时,spy还不存在。您的代码相当于:

var c = new MyClass()
var spy = sinon.spy(c, "myFunc");
显然,当调用构造函数时,间谍不在适当的位置

要解决此问题,可以在
MyClass
对象的原型中移动方法
myFunc
,然后在原型中监视方法

例如:

function MyClass() {
  this.init();
}

MyClass.prototype.myFunc = function() {
    console.log("hi");
}

MyClass.prototype.init = function() {
   this.myFunc();
}

var myFuncSpy = sinon.spy(MyClass.prototype, "myFunc");
var initSpy = sinon.spy(MyClass.prototype, "init");

var c = new MyClass();
console.log(myFuncSpy.called); // TRUE
console.log(initSpy.called);  // TRUE
JSFIDDLE:
打开控制台查看结果。

这里的问题是,当调用方法
myFunc
时,spy还不存在。您的代码相当于:

var c = new MyClass()
var spy = sinon.spy(c, "myFunc");
显然,当调用构造函数时,间谍不在适当的位置

要解决此问题,可以在
MyClass
对象的原型中移动方法
myFunc
,然后在原型中监视方法

例如:

function MyClass() {
  this.init();
}

MyClass.prototype.myFunc = function() {
    console.log("hi");
}

MyClass.prototype.init = function() {
   this.myFunc();
}

var myFuncSpy = sinon.spy(MyClass.prototype, "myFunc");
var initSpy = sinon.spy(MyClass.prototype, "init");

var c = new MyClass();
console.log(myFuncSpy.called); // TRUE
console.log(initSpy.called);  // TRUE
JSFIDDLE:
打开控制台查看结果。

非常感谢您的回答-我从来没有想到过这个解决方案-现在我的代码中可以使用它-非常感谢:)小提琴很棒-如果我付钱给您,就不会有更好的答案了!一位真正的绅士非常感谢你的回答——我从来没有想到过这个解决方案——现在我的代码中有了这个功能——非常感谢:)提琴很棒——如果我付钱给你,我不可能期待一个更好的答案!真正的绅士