简体   繁体   中英

AngularJS Jasmine test: TypeError: 'undefined' is not an object

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

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

Looks like you are using DeliveriesService in your controller.

Whenever you are using a service, you need to make sure either you inject that service or add it using a $provider.

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

or

beforeEach(module(function ($provide) {
     mockObj = {
        functionName: jasmine.createSpy('functionName')
     }
     $provide.value('DeliveriesService',mockObj)
}));

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