2016-04-09 33 views
2

新角和以下從我以前的帖子angularjs jasmine tests: Variable vm not found 我在我的角度測試有一個TypeError,並不知道什麼問題是。下面是我的測試:AngularJS茉莉花測試:TypeError:'undefined'不是一個對象

(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 
+3

你能顯示控制器的代碼?這將有助於更好地理解問題。看起來'DeliveriesController'不僅取決於'$ scope',還取決於'DeliveriesService'。當你實例化控制器時,你應該提供它所有的依賴關係(無論是真實的還是模擬的)。 – Stubb0rn

+1

你說得對。情況就是如此。我添加了DeliveriesService作爲注入到測試中,它工作。謝謝你 – noobcoder

+0

@ Stubb0rn請發表您的評論作爲答案。它真的幫了我,但如果你把它作爲評論,它不會很容易找到。 –

回答

0

禮貌:從@StubbbOrn評論:

你能顯示控制器的代碼?這將有助於更好地理解問題。看起來DeliveriesController不僅取決於$範圍,還取決於DeliveriesService。當你實例化控制器時,你應該提供它所有的依賴關係(無論是真實的還是模擬的)。

該解決方案適用於我,是解決方案。謝謝@StubbOrn

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