Two things, you need your test
function to return
the Promise and you need to use an arrow function or .bind
the callback to the parent function (otherwise the this
in this.someVariable
will refer to the callback function):(有两件事情,你需要你的test
函数return
的承诺,你需要使用一个箭头功能或.bind
回调到母函数(否则this
在this.someVariable
将参照回调函数):)
this.test = function() {
return this.functionWhichReturnsPromise().then(() => {
this.someVariable = "new value";
});
}
or(要么)
this.test = function() {
return this.functionWhichReturnsPromise().then(function() {
this.someVariable = "new value";
}.bind(this));
}
Then in your test you can do:(然后在测试中,您可以执行以下操作:)
it("should test the function test", function (done) {
var abc = new ABC();
abc.test().then(function() {
expect(abc.someVariable).toBe('new value');
done();
});
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…