Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test variables local to an Angular controller in Jasmine

I have a controller with a local variable

function IndexCtrl($scope) {
  var pagesById = [];
  loadPages();

  // snip

  function loadPages() {
    // pagesById gets populated
  }

  // snip
}

I'd like to test that pagesById is correctly populated but I'm not sure how to get at it from my it(). I don't need this variable to be in the $scope, it's just an intermediate set of information, so if I can avoid adding it to $scope that would be ideal.

it('scope.pages should populated based on pages data.', function() {
    $httpBackend.flush();
    expect(pagesById).toEqualData(mock_page_results);
});

gives me

ReferenceError: pagesById is not defined

Do I have any other options besides attaching it to $scope?

like image 456
akronymn Avatar asked Jan 10 '13 23:01

akronymn


People also ask

What controller do you use in Angular to mock remote data for testing?

The HttpClientTestingModule allows you to easily mock HTTP requests by providing you with the HttpTestingController service.

What does detectChanges do in Angular Jasmine tests?

detectChanges() tells Angular to run change-detection. Finally! Every time it is called, it updates data bindings like ng-if, and re-renders the component based on the updated data. Calling this function will cause ngOnInit to run only the first time it is called.

What is Jasmine testing framework and how do you use it for Angular unit testing?

Jasmine is a behavior development testing framework. Unit tests are written using Jasmine and are run to see if individual parts of an application are working correctly. As a result, unit tests will either pass or fail depending on if the code is working correctly or has a bug.


3 Answers

In your jasmine spec, first create the controller:

var ctrl;

beforeEach(inject(function($rootScope, $controller) {
  scope = $rootScope.$new();
  ctrl = $controller('myController', {
     $scope: scope
  });
}));

Then you can access its properties by doing ctrl.pagesById. Of course, rather than doing var pagesById you'll need to use this.pagesById in your controller.

like image 119
wless1 Avatar answered Sep 17 '22 23:09

wless1


Here is my way for testing local variables in Angular methods:

//------- Function for getting value of local variables

function getLocalVariable(sLocalVarName, obj, method, args){
    method = method.toString();
    let changedMethod = method.substring(0, method.lastIndexOf("}")) + ";" + "return " + sLocalVarName + "}";
    eval(' changedMethod = ' + changedMethod);

    return changedMethod.call(obj, args)
}

//----------- service

class assignStuffService {

    getAvaliableStuff(shift) {
        const params = {
            agency_id: 0 ,
            start_time: shift.data.shift.start_time,
            end_time : shift.data.shift.end_time
        };
    }
}

//----------- part of spec

it('should set values to "params" object props', () => {

            let shift = {
                data:{
                    shift:{
                        start_time:'',
                        end_time:''
                    }
                }
            };

            let params = getLocalVariable('params',
                                            assignStuffService,
                                            assignStuffService.getAvaliableStuff,
                                            shift);

            expect(params).toEqual({
                agency_id: 0 ,
                start_time: shift.data.shift.start_time,
                end_time : shift.data.shift.end_time
            });

        });
like image 38
victor mozharovsky Avatar answered Sep 17 '22 23:09

victor mozharovsky


Here is my way for testing local variables in Angular methods:

//------- Function for getting value of local variables

function getLocalVariable(sLocalVarName, obj, method, args){
    method = method.toString();
    let changedMethod = method.substring(0, method.lastIndexOf("}")) + ";" + "return " + sLocalVarName + "}";
    eval(' changedMethod = ' + changedMethod);

    return changedMethod.call(obj, args)
}

//----------- service

class assignStuffService {

    getAvaliableStuff(shift) {
        const params = {
            agency_id: 0 ,
            start_time: shift.data.shift.start_time,
            end_time : shift.data.shift.end_time
        };
    }
}

//----------- part of spec

it('should set values to "params" object props', () => {

            let shift = {
                data:{
                    shift:{
                        start_time:'',
                        end_time:''
                    }
                }
            };

            let params = getLocalVariable('params',
                                            assignStuffService,
                                            assignStuffService.getAvaliableStuff,
                                            shift);

            expect(params).toEqual({
                agency_id: 0 ,
                start_time: shift.data.shift.start_time,
                end_time : shift.data.shift.end_time
            });

        });
like image 24
Victor Mozharovsky Avatar answered Sep 16 '22 23:09

Victor Mozharovsky