vendredi 24 juin 2016

Angular $scope method testing

I'm trying to test if a $scope method has been called

my controller :

(function () {
    var app = angular.module('productsController',[]);
      app.controller('AddProductController', ['$scope',function ($scope) {
        $scope.test = function(){
          return true;   
        };
        $scope.test();
    }]);
})();

and my test :

describe("Products Controller", function () {
    beforeEach(function () {
        module('productsController');
    });

    beforeEach(function () {
        var  createController, scope, rootScope;

    });
    describe('AddProductController', function () {
        beforeEach(function () {
            inject(function ($controller, _$rootScope_) {
                rootScope = _$rootScope_;
                scope = _$rootScope_.$new();
                createController = function () {
                    return $controller("AddProductController", {
                        $scope: scope,
                    });
                };
            });
        });

        it('should call test', function(){
            spyOn(scope, 'test');
            createController();
            scope.$apply()
            expect(scope.test).toHaveBeenCalled();
        })
    });
});

but I got this error:

Error: test() method does not exist

$scope.test exists any when I run the app in a browser the method is been called, but if falis whne testing, any clues?

Aucun commentaire:

Enregistrer un commentaire