2014-04-23 42 views
11

Jasmine ve karma için yeni bir markayım. Ortam kurulumuna uygun bir şekilde sahip olduğuma ve çok temel birim testlerini yürütebileceğime inanıyorum, fakat bir denetleyiciyi başlatmaya çalıştığımda, bilinmeyen bir sağlayıcı hatası verdim ve bunun nasıl hata ayıklandığından emin değilim. Bir stateProvider bağımlılığı geçirmem gerekiyor mu? Bunu açısal tohum örneğinde görmüyorum.Birim Test bağımlılığı enjeksiyonu

Bower.json:

{ 
"name": "starter", 
    "description": "A starter project for AngularJS", 
    "version": "2.0.0", 
    "homepage": "https://starter.com", 
    "private": true, 
    "dependencies": { 
     "angular": "1.2.x", 
     "angular-route": "1.2.x", 
     "angular-loader": "1.2.x", 
     "angular-mocks": "~1.2.15" 
    } 
} 

Ev Denetleyici:

angular.module('home').controller('Home', function($scope, $rootScope, $state) { 

    console.log($scope.pageType); 

    $rootScope.pageType = 'home'; 

    /* 
    * Takes in a state and transitions the app to that state. 
    */ 
    $scope.goTo = function(value) { 
     $state.transitionTo(value); 
    } 

    /* 
    * Handles what happens after clicking log-in 
    */ 
    $scope.loginClicked = function() { 
     $state.transitionTo('log-in'); 
    } 
}); 

Testi dosyası:

'use strict'; 

/* jasmine specs for controllers go here */ 

describe('Home', function() { 
    beforeEach(module('home')); 

    it('should run tests', inject(function() { 
     expect(null).toBeDefined(); 
    })); 

    it('should not say true equals false', function() { 
     expect(false).not.toBe(true); 
    }); 

    it('should say true equals true', function() { 
     expect(true).toBe(true); 
    }); 

    it('should say false does not equal true', function() { 
     expect(false).not.toBe(true); 
    }); 

    it('should create "phones" model with 3 phones', inject(function($controller,$rootScope) { 

    /* 
    * 
    * COMMENTING OUT THESE LINES = PASS 
    * 
    */ 
     var scope = $rootScope.$new(), 
      ctrl = $controller('Home', {$scope:scope}); 
     expect(ctrl).not.toBe(null); 
    })); 

}); 

Hata:

Error: [$injector:unpr] Unknown provider: $stateProvider <- $state 
http://errors.angularjs.org/1.2.16/$injector/unpr?p0=%24stateProvider%20%3C-%20%24state 
    at /Users/jlett/bower_components/angular/angular.js:78:12 
    at /Users/jlett/bower_components/angular/angular.js:3705:19 
    at Object.getService [as get] (/Users/jlett/bower_components/angular/angular.js:3832:39) 
    at /Users/jlett/bower_components/angular/angular.js:3710:45 
    at getService (/Users/jlett/bower_components/angular/angular.js:3832:39) 
    at invoke (/Users/jlett/bower_components/angular/angular.js:3859:13) 
    at Object.instantiate (/Users/jlett/bower_components/angular/angular.js:3880:23) 
    at /Users/jlett/bower_components/angular/angular.js:7134:28 
    at null.<anonymous> (/Users/jlett/test/unit/home-controller_tests.js:26:20) 
    at Object.invoke (/Users/jlett/bower_components/angular/angular.js:3869:17) 
Error: Declaration Location 
    at window.inject.angular.mock.inject (/Users/jlett/bower_components/angular-mocks/angular-mocks.js:2132:25) 
    at null.<anonymous> (/Users/jlett/test/unit/home-controller_tests.js:24:54) 
    at /Users/jlett/zoetis-rimadyl-mobile/test/unit/home-controller_tests.js:5:1 
+0

Karma yapılandırmanızda açısal yönlendiriciyi bir şekilde dahil etmemeniz mümkün mü? Aslında bunu bower.json'da görmüyorum. Ui yönlendirici dahil mi? – aet

cevap

36

Eğer enjektabl modülünden biri dahil değilse, bu hatayı alırsınız.

Örneğin, sizin $state bağımlılık home modülünde değilse

beforeEach(module('home')); 

, ayrıca bu modülü eklemeniz gerekir var. Ben $state aşina değilim (Ben angular-ui yönlendirici olduğunu düşünüyorum? Sadece angular.js hizmetleri $ ile başlaması gerekiyordu).

beforeEach(module('ui.router')); 
beforeEach(module('home')); 

Bu şekilde, en açısal testi atlet modülleri testler gereklidir bilir: Bu açısal ui ise, bu nasıl yapmanız gerekir kurulduğundan.

Gerçekten, bu modülün bağımlılığı olarak tanımlanan ui.router bağımlılığı olduğu sürece, home modülünün eklenmesi sizin için bunu yapmalıdır. Doğru şekilde yapılandırdıysanız, dosyalarınızın sırasına göre sınava girme sırasına bakmanız gerekebilir. Örneğin, ui yönlendirici dosyasının testlerinize dahil edildiğinden ve karma yapılandırmasında home modülünüzden önce referans alındığından emin olun.

+2

Aynı konuyla ilgili olarak, ana uygulama modülünü beforeEach (modül ('com.my.project'))) eklemeyi seçtim; var app = angular.module ('com.my.project', [ 'ngAnimate', ..., 'app.oneDomainModule', 'app.anotherDomainModule' ]); – Stephane

8

Denetleyicinizdeki $state bağımlılığını eklediğiniz için - denetleyici birim testine $state değerini sağlamanız gerekir. değişikliklerle

var $scope = $rootScope.$new(), 
    ctrl = $controller('Home', { 
       $scope: $scope, 
       $state: {} //Or inject the state using the injector service and then you can use some jasmine spies to mock the function calls or just to spy on 'em. 
      }); 
    expect(ctrl).not.toBe(null); 

senin o blok .... ben bu kurulum here tarif olarak kontrolör kurulumu için işlev oluşturmak ister benim birim testlerin kurulumda Ancak

it('should create "phones" model with 3 phones', inject(function($controller, $rootScope, $state) { 

    /* 
    * 
    * COMMENTING OUT THESE LINES = PASS 
    * 
    */ 
     var scope = $rootScope.$new(), 
      ctrl = $controller('Home', {$scope:scope, $state: $state}); 
     expect(ctrl).not.toBe(null); 
    })); 

.