jasmine单元测试中未调用存根
获取一个错误,该错误表明Dsub已被调用0次。有人能帮我解决这个问题吗?如果不了解更多关于代码的信息,很难说出来,但这行代码看起来并不像你认为的那样简单:jasmine单元测试中未调用存根,jasmine,jasmine-jquery,jasmine-node,Jasmine,Jasmine Jquery,Jasmine Node,获取一个错误,该错误表明Dsub已被调用0次。有人能帮我解决这个问题吗?如果不了解更多关于代码的信息,很难说出来,但这行代码看起来并不像你认为的那样简单: function fun{ A.B.C().D(someconstant); $(element).prop("checked",false).trigger("change"); } describe(()=>{ let triggerStub: Sinon.SinonStub;
function fun{
A.B.C().D(someconstant);
$(element).prop("checked",false).trigger("change");
}
describe(()=>{
let triggerStub: Sinon.SinonStub;
let Dstub: Sinon.SinonStub;
beforeEach(() => {
triggerStub = sandboxInstance.stub($.fn, "trigger");
Dstub = sandboxInstance.stub(A.B.C(),"D");
});
it("Verification",()=>{
fun();
sinon.assert.calledOnce(Dstub);
sinon.assert.calledWithExactly(triggerStub,"change");
});
这似乎是在一次调用A.B.C()
,而不是在另一次调用上中断D
函数。换句话说,fun
中的A.B.C()
与您之前的中的A.B.C()
不同,因此您没有截取正确的内容
如果您可以存根A.B.C()
返回的任何对象的原型,这可能会解决您的问题
您还可以存根A.B.C()
的结果,以返回所需的Dstub
:
Dstub = sandboxInstance.stub(A.B.C(),"D");
希望有帮助 谢谢你的帮助!:)你是说sandboxistance.stub(A.B,“C”).returns({D:Dstub})?此外,断言将保持不变,对吗?@user2597100是的,如果我理解正确,断言应该保持不变。感谢帮助:)这很好。:)你能澄清我的另一个疑问吗?如果A.B.C()?
describe(() => {
let triggerStub: Sinon.SinonStub;
let Dstub: Sinon.SinonStub;
beforeEach(() => {
triggerStub = sandboxInstance.stub($.fn, "trigger");
// Create the stub for D.
DStub = sandboxInstance.stub();
// Make A.B.C() return that stub.
sandboxInstance.stub(A.B, 'C').returns({
D: Dstub
});
});
// ...