I have a scenario where I need to test controller method using Karma Jasmine. Scenario is simple,
Angular code
var globalVariable = "startEapplication";
$scope.myMethod = function(request, response) {
if(globalVariable === "startEapplication" && request.customer) {
Now, instead of startEapplication I need to pass null for the negative scenario testing. Basically I need to mock the global variable from Karma Jasmine.
Is there anyway to modify the Angular global variable from Karma Jasmine.
Firstly, globalVariable is not a angular global variable and since it is declared inside controller you will not be able to access from the spec itself.
And if you want to cover the negative scenario I used to follow below approach to test the private variable.
To test the negative scenario, you can declare one separate private object and associate it with scope.
Example:
// Add the comment saying this should be only be accessed for unit test puropse
$scope._private_ = {
globalVariable : "startEapplication"
}
Also you need to do the changes the way you are comparing it so that the spec will also point to same object like
if($scope._private_.globalVariable === "startEapplication")
Now you can mock this global variable inside your spec.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.