简体   繁体   中英

Mocking a dependent service to a controller unit test in AngularJS

I have a problem when unit testing an Ionic app. I have seen similar questions, but none solved my problem.

Basically, this is my controller.js

angular.module('starter.controllers')
.controller('myController', function($scope, myService) {

    $scope.test = function() {
        return myService.getTestVariable();
    };

});

and this is my service.js

angular.module('starter.services')
.factory('myService', function() {

    var self = this;
    self.testVariable = true;

    return {
        getTestVariable : function() {
            return self.testVariable;
        }
    };
});

Modules are created in a app.js file:

angular.module('starter', ['starter.controllers', 'starter.services']);           
angular.module('starter.services', []);
angular.module('starter.controllers', []);

Now, I want to test the controller and use a mock versione of the service, so I set up the test like this:

describe('Controller: myController', function() {
    var scope, controller, serviceMock;

    beforeEach(function() {
        module('starter');
    });

    beforeEach(function () {
        serviceMock= {
            getTestVariable : function() {return true;}
        };

        module(function ($provide) {
            $provide.value('myService', serviceMock);
        });
    });

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

    it('should work as expected', function(){
        spyOn(serviceMock, 'getTestVariable').and.callThrough();
        scope.test();
        expect(serviceMock.getTestVariable).toHaveBeenCalled();
    });
});

However, when I try to run the test, it prints an error:

TypeError: 'undefined' is not a function (evaluating 'myService.getTestVariable()')
    at <dir>/controller.js:5

Why is this happening?

I don't see any problem in the code, just do module initialisation angular.module('starter.services', []); angular.module('starter.controllers', []); angular.module('starter.services', []); angular.module('starter.controllers', []); before its used.

See the below example. same code you provided works perfectly.

 angular.module('starter.controllers', []); angular.module('starter.controllers') .controller('myController', function($scope, myService) { $scope.test = function() { return myService.getTestVariable(); }; }); angular.module('starter.services', []); angular.module('starter.services') .factory('myService', function() { var self = this; self.testVariable = true; return { getTestVariable: function() { return self.testVariable; } }; }); angular.module('starter', ['starter.controllers', 'starter.services']); describe('Controller: myController', function() { var scope, controller, serviceMock; beforeEach(function() { module('starter'); }); beforeEach(function() { serviceMock = { getTestVariable: function() { return true; } }; module(function($provide) { $provide.value('myService', serviceMock); }); }); beforeEach(inject(function($rootScope, $controller) { scope = $rootScope.$new(); controller = $controller('myController', { $scope: scope }); })); it('should work as expected', function() { spyOn(serviceMock, 'getTestVariable').and.callThrough(); scope.test(); expect(serviceMock.getTestVariable).toHaveBeenCalled(); }); }); 
 <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.2.1/jasmine.min.css"> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.2.1/jasmine.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.2.1/jasmine-html.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.2.1/boot.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-mocks.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.min.js"></script> 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM