如何存根类属性

Rol*_*wal 5 javascript sinon

我正在尝试使用 sinon 来存根类属性。

function wrapper() {
  this.obj = {"message":"hello"};
  this.sendmessege = function() {
    console.log(this.obj.message);
    return "message is:" + this.obj.message;
  } 
 }

// stub

var wrap = new wrapper();
stub = sinon.stub(wrap , 'sendmessege', function () { 
    return 'hola'; 
 });
 stub1 = sinon.stub(wrap , {'obj':
   {'message':'hii'}
 });

console.log(stub1);
Run Code Online (Sandbox Code Playgroud)

它给了我

错误:尝试将对象属性 obj 包装为函数。

我如何存根 obj ?

Tri*_*ell 6

如果要存根对象的属性,请value()使用Stub.

stub1 = sinon.stub(wrap, 'obj').value({message: 'hii'});
Run Code Online (Sandbox Code Playgroud)

文档参考:http://sinonjs.org/releases/v4.1.2/stubs/#stubvaluenewval