0

I have a function like this inside a controller

$scope.closeMenu = function () {
                    $('#menu').hide();
                };

If a functions returns a value I am able to test using expect($scope.value).toEqual(); How do we test the above function using jasmine. Please help thanks

1 Answer 1

1

If a functions returns a value I am able to test using expect($scope.value).toEqual(); How do we test the above function using jasmine

You should rewrite your function, so as it only set a variable of the model. Then it will be testable using the way you already know in Jasmine. One of the point of angular is that you don't manipulate the DOM from the controller. Providing you follow these guidelines, controllers are far easier to test.

Moreover, in your case rewriting is very straightforward:

$scope.closeMenu = function () {
    $scope.isOpen = false;
};

Template:

... id="menu" ng-show="isOpen" ...

In case you still need to test some characteristics of a DOM element, for example it's visibility, jasmine-jquery might be useful for you. Example:

expect($('#menu')).toBeHidden()
Sign up to request clarification or add additional context in comments.

5 Comments

For the code I have is it impossible to write unit test? I do not want to change the code and trying to figure out a way to test this
Sure it's possible, but not with Jasmine. You would need some addition to it.
May I know what is the addition needed?
jasmine-jquery for example. Then an expectation like expect($('#menu')).toBeHidden() would be possible.
thanks.. I got to know how to solve my problem but I am having issues in using jasmine-jquey. stackoverflow.com/questions/33248952/using-jasmine-jquery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.