在茉莉花单元测试中没有被调用

时间:2016-01-02 19:48:16

标签: jasmine jasmine-jquery jasmine-node

 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");
        });

获取Dstub被调用0次的错误。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

在不了解您的代码的情况下很难说清楚,但看起来这条线并不能阻止您认为它的存根:

Dstub = sandboxInstance.stub(A.B.C(),"D");

这似乎是在D一个调用上的A.B.C()函数,而不是另一个<{1}}。换句话说,A.B.C()中的fun与您A.B.C()中的beforeEach不一样,因此您并没有找到正确的内容。

如果您可以存储任何A.B.C()返回的原型,那么这可能会解决您的问题。

您还可以存根A.B.C()的结果,以便返回您想要的Dstub

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
    });
  });

  // ...

希望有所帮助!