簡體   English   中英

AngularJS單元測試:注入服務未定義

[英]AngularJS Unit Tests: Injected Services undefined

由於噴油器沒有正確噴射,我對單元測試angularJS有困難。 我注入的任何東西都是undefined

模塊:

app.module.js

angular.module('myServices', []);

AngularTest.js

describe('AngularTest', function() {

    beforeEach(module('myServices'));

    var scope;

    beforeEach(inject(function(_$rootScope_) {
        // The injector unwraps the underscores (_) from around the parameter names when matching
        scope = _$rootScope_;

    }));

    describe('Testing Angular injection', function() {

      it('scope should be defined', function() {    
        expect(scope).toBeDefined(); // fails
      });

    });
});

我已經在我的主模塊中成功測試了控制器,但是,這個特定的模塊正在測試我的理智。

有任何想法嗎 ?

編輯:

業力文件:

files: [
      'app/bower_components/angular/angular.js',
      'app/bower_components/angular-resource/angular-resource.js',
      'app/bower_components/angular-route/angular-route.js',
      'app/bower_components/angular-animate/angular-animate.js',
      'app/bower_components/angular-cookies/angular-cookies.js',
      'app/bower_components/angular-mocks/angular-mocks.js',
      'app/app.module.js',
      'app/components/**/controller/*.js',
      'app/components/**/model/*.js',
      'app/components/**/test/unit/*.js'
    ]

服務模塊在app.module.js定義,服務在app/components/services/model/myService.js (路徑似乎不合邏輯,我試圖抽象我的結構盡可能地概括這個問題)

編輯2:

Karma似乎也沒有抱怨:從日志中:

28 08 2015 03:38:47.095:WARN [karma]: No captured browser, open http://localhost:9876/
28 08 2015 03:38:47.107:INFO [karma]: Karma v0.13.9 server started at http://localhost:9876/
28 08 2015 03:38:47.151:INFO [launcher]: Starting browser Safari
28 08 2015 03:38:54.790:INFO [Safari 8.0.6 (Mac OS X 10.10.3)]: Connected on socket jhi7nBcWAV_vCJkMAAAA with id 68159057

接下來是失敗的測試用例:

Safari 8.0.6 (Mac OS X 10.10.3) AngularTest Testing Angular injection scope should be defined FAILED
    /path/to/project/app/bower_components/angular/angular.js:68:32
    forEach@/path/to/project/app/bower_components/angular/angular.js:336:24
    loadModules@/path/to/project/app/bower_components/angular/angular.js:4369:12
    createInjector@/path/to/project/app/bower_components/angular/angular.js:4294:22
    workFn@/path/to/project/app/bower_components/angular-mocks/angular-mocks.js:2390:60
    /path/to/project/app/bower_components/angular/angular.js:4408:30
    Expected undefined to be defined.
    /path/to/project/app/components/services/test/unit/AngularTest.js:16:34

親你SAFARI!

解決方案:在karma配置文件中將瀏覽器更改為Chrome,調試日志會針對錯誤消息提供更詳細的信息!

原來我沒有在我的業力files包含所需的模塊依賴項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM