AngularJS Jasmine测试:TypeError:'undefined'不是对象

时间:2016-04-09 00:09:02

标签: javascript angularjs jasmine

Angular新手,并从angularjs jasmine tests: Variable vm not found的早期帖子中跟进 我在角度测试中遇到TypeError而不确定问题是什么。这是我的测试:

(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);
            });
        });
    });

  });

})();

我得到的错误如下:

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

2 个答案:

答案 0 :(得分:0)

礼貌:来自@StubbbOrn的评论:

你能展示控制器的代码吗?这将有助于更好地理解问题。看起来DeliveriesController不仅取决于$ scope,还取决于DeliveriesService。当您实例化控制器时,您应该提供所有依赖项(实际或模拟的)。

此解决方案对我有用,是解决方案。谢谢@StubbOrn

答案 1 :(得分:0)

您的控制器中似乎正在使用DeliveriesService

每当您使用某项服务时,您需要确保注入该服务或使用$ provider添加该服务。

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

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