Jacob Nelson Jacob Nelson - 3 years ago 145
Javascript Question

jasmine test javascript inner function

Here is my javascript code

function organization() {
var self = this;

function activate() {
// statements
}
}


I know the scope of the method activate() remains inside the method organization() and that is the challenge I am facing.

My requirement is to write a test in Jasmine to ensure method activate have been called.

it('activate method defined?', function() {
expect(activate()).toBeDefined();
});


but this test is failing with the message


TypeError: activate is not a function


Any inputs to make the above test success will be highly helpful.

Answer Source

You don't need to check that private function 'activate' actually called or defined. You need to check that public function 'organization' as I understand is called or defined and if activate set some public properties etc. For example:

function organization() {
    var vm = this;
    activate();        
    function activate() {
        vm.activated = true;
    }
}

it('should activate organization', function() {
    organization();

    expect(vm.activated).toBe(true);
});

Or if you need, you can make function activate as public:

function organization() {
   var vm = this;
   vm.isActivated = false;
   vm.activate = activate;

   function activate() {
       vm.isActivated = true;
   }
}

it('should activate organization', function() {
    vm.activate();

    expect(vm.isActivated).toBe(true);
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download