Ad

AngularJS Jasmine Test: TypeError: 'undefined' Is Not An Object

- 1 answer

New to Angular and following up from my earlier post from angularjs jasmine tests: Variable vm not found I am having a TypeError in my angular tests and not sure what the problem is. Here is my test:

(function(){
'use strict';
describe('Testing DeliveriesController', function() {

    beforeEach(module('app.deliveries'));

    describe('Testing deliveries controller', function(){
        var vm, controller;

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

        afterEach(function() {
            vm = undefined;
            controller = undefined;
        });

        describe('priorities length', function(){
            it('it should test priority length', function () {
                expect(vm.priorities.length).toBe(0);
            });
        });
    });

  });

})();

The error I get is as follows:

PhantomJS 1.9.8 (Mac OS X 0.0.0) Testing DeliveriesController Testing deliveries controller priorities length it should test priority length FAILED
Error: [$injector:unpr] Unknown provider: DeliveriesServiceProvider <- DeliveriesService <- DeliveriesController
http://errors.angularjs.org/1.3.20/$injector/unpr?p0=DeliveriesServiceProvider%20%3C-%20DeliveriesService%20%3C-%20DeliveriesController
    at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4031
    at getService (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4178)
    at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4036
    at getService (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4178)
    at invoke (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4210)
    at instantiate (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4227)
    at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:8524
    at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular-mocks/angular-mocks.js:1916
    at /Users/rgoti/ingestion/external-ingestion/app/public/src/app/deliveries/deliveries.spec.js:12
    at invoke (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4219)
    at workFn (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular-mocks/angular-mocks.js:2475)
undefined
TypeError: 'undefined' is not an object (evaluating 'vm.priorities.length')
    at /Users/rgoti/ingestion/external-ingestion/app/public/src/app/deliveries/deliveries.spec.js:23
Ad

Answer

Courtesy: comment from @StubbbOrn:

Could you show controller's code? It would help to understand problem better. Looks like DeliveriesController depends not only on $scope but also on DeliveriesService. When you instantiate controller you should provide it all dependencies (either real or mocked ones).

This solution worked for me and was the solution. Thanks @StubbOrn

Ad
source: stackoverflow.com
Ad