How to mock variable inside the function which i'm trying to test ?

792 views
Skip to first unread message

Jai Kumar

unread,
Nov 1, 2018, 11:00:34 AM11/1/18
to Jasmine

--Source Code

function myFunction() {
    if (flag) {
        function_1();
    }
    else {
        function_2();
    }
};

--Jasmine Spec

/// <reference path="scripts/myFunctions.js" />
    describe('Test Suite 1:', function() {
       var functions;
       beforeEach(function () {
            functions = {
                myFunction: myFunction,
                function_1: function_1
            }
            spyOn(functions, 'myFunction').and.callThrough();
            spyOn(functions, 'function_1');
       }

       it('should call function_1 is flag is true', function () {
          //When: I call myFunction()
          functions.myFunction();

          //Then: I verify function_1 called once
          expect(functions.function_1.calls.count()).toEqual(1);
       });
    });

In above code flag is a bool variable, when i run test case it's not able to find reference to that variable. How can i make it work ?


Jasmine Spec failing with Error: Reference Error “flag” is undefined

Tony Brix

unread,
Nov 2, 2018, 10:27:25 AM11/2/18
to Jasmine
Where is that flag initialized?
Reply all
Reply to author
Forward
0 new messages