TechLiam
TechLiam

Reputation: 147

Testing a function inside a function with Jasmine

I have a function that has a function inside it I need to be able to test that the inner function gets called I'v tryed looking for examples of spyOn but not quite found anything I could us the code im trying to test is like

function OutterFunc(){
    function InnerFunc(){
        return "A";
    }
    var b = InnerFunc();
    return b;
}

The test i wish i could do is that InnerFunc has been called Any help would be really really welcome

Upvotes: 1

Views: 3340

Answers (1)

Robert Moskal
Robert Moskal

Reputation: 22553

Hmm, I don't see any way obvious way of testing whether that inner function was called. In the trivial case you give above, I suppose getting a return value of "A" is pretty compelling proof that the function was called.

If testing to make sure a function was called in a certain way, under certain conditions you'd have to refactor your code. You could move the inner function out of the other one as in the comment above. You might pass the inner function into the outer one like so:

var inner = function(){
    return 'A';
} 

function OuterFunc(fn) {
    return fn();
}

And then call it like so:

var res = OuterFunc(inner);

Now you can spy on inner. There are other possibilities, but you are going to have to do some refactoring to get that inner function tested.

Upvotes: 1

Related Questions