茉莉花-如何监视没有实现的函数

时间:2018-08-01 13:45:55

标签: javascript unit-testing jasmine

我有要测试的脚本(playground.efs)。但是,它使用eSignal API,因此我必须监视其中的任何功能。

我在设置spyOn时遇到了麻烦,我不知道该反对什么。通过测试testEfsCode(),需要创建getCurrentBarIndex()的间谍。由于getCurrentBarIndex()不在任何对象之内,我该如何对其进行存根处理?

我当前代码遇到的错误是:

Error: <spyOn> : getCurrentBarIndex() method does not exist
Usage: spyOn(<object>, <methodName>)

TestSpec.js

var util = require('../../../Interactive Data/Formulas/My Formulas/playground.efs');

describe("Utils", function() {
      it("should return true", function() {
            var spy = spyOn(util.testEfsCode, 'getCurrentBarIndex').and.callThrough();
            expect(util.testEfsCode()).toBe(true);
      });
});

playground.efs(要测试的脚本)

function testEfsCode(){
    getCurrentBarIndex();
    return true;
}

// Check For EFS
if ( typeof module !== 'undefined' && module.hasOwnProperty('exports') )
{
    module.exports = {
        testEfsCode: testEfsCode
    }
}

getCurrentBarIndex()是eSignal提供的功能,所以我没有代码。该函数仅在脚本加载到eSignal中时运行。

1 个答案:

答案 0 :(得分:1)

我能够从这个问题Using Jasmine to spy on a function without an object找出解决方案

我所要做的就是使用createSpy

getCurrentBarIndex = jasmine.createSpy().and.returnValue(8);
expect(util.testEfsCode()).toBe(8);