1

I have my script (playground.efs) which I want to test. However, it uses the eSignal API, so I have to spy any of those functions.

I'm having trouble setting up spyOn, I don't know what to object to spy on should be. With testing testEfsCode(), a spy for getCurrentBarIndex() needs to be created. Since getCurrentBarIndex() isn't apart of any object how can I stub it?

The error I'm getting with my current code is:

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 (Script To Test)

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

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

getCurrentBarIndex() is a function supplied by eSignal, so I don't have the code for it. The function only runs when the script is loaded into eSignal.

DevEng
  • 1,114
  • 2
  • 15
  • 29
  • Do you have any documentation for `getCurrentBarIndex`? Where is it? Is it a global function? Have you tried spying with `spyOn(window, "getCurrentBarIndex")`? – zero298 Aug 01 '18 at 14:24
  • `getCurrentBarIndex()` is a function supplied by eSignal. You can only call it when you run the script inside of the eSignal application. Also, I'm not using `window` in my code so I get ReferenceError: window is not defined – DevEng Aug 01 '18 at 14:33

1 Answers1

2

I was able to figure out the solution from this question Using Jasmine to spy on a function without an object

All I had to do was use createSpy

getCurrentBarIndex = jasmine.createSpy().and.returnValue(8);
expect(util.testEfsCode()).toBe(8);
DevEng
  • 1,114
  • 2
  • 15
  • 29