jeudi 7 mai 2015

How to test the variable in Angular

I need to test the local variable of a function in my case.

I have something like

Test Controller file

  $scope.fetchName = function() {
            var name, id;
            //after bunch of calculation codes...
            name = 'Ben';
            id = 123;
   }

I know I can test them if I use $scope.name and $scope.id but I don't want to because they don't need to be assigned to the scope.

My test

describe('Test', function () {
    var testCtrl, scope;

    beforeEach(module('testApp'));

    beforeEach(inject(function (_$controller_, _$rootScope_) {
        $rootScope = _$rootScope_;
        scope = _$rootScope_.$new();

        testCtrl = _$controller_('testCtrl', {
            $scope: scope
        });    
    }));

    describe('Test local', function () {
       it('should test local variable', function(){
           scope.fetchName();
           expect(name).toBe('Ben');
           expect(id).toBe(123);
       })
    });
});

I am getting error when I run the test.

Can't find variable: name

Can anyone help me about the issue? Thanks!

Aucun commentaire:

Enregistrer un commentaire