2016-03-11 107 views
1

我正在開發一個角度應用程序。我有應用程序與少數控制器和服務運行。現在我想開始對服務和控制器進行單元測試。但不知道我做錯了什麼,我無法正確運行一項測試甚至一項服務。當我嘗試使用使用因果報應來運行測試:
因緣開始karma.conf.js

我獲得以下錯誤:

Firefox 38.0.0 (Windows 7 0.0.0) service: MyCategoryService should send a request to MyCategoryService FAILED 
    minErr/<@c:/Users/bgurung2/workspace/admintoolui/src/main/webapp/bower_components/angular/angular.js:68:12 
    loadModules/<@c:/Users/bgurung2/workspace/admintoolui/src/main/webapp/bower_components/angular/angular.js:4458:15 
    [email protected]:/Users/bgurung2/workspace/admintoolui/src/main/webapp/bower_components/angular/angular.js:340:11 
    [email protected]:/Users/bgurung2/workspace/admintoolui/src/main/webapp/bower_components/angular/angular.js:4419:5 
    [email protected]:/Users/bgurung2/workspace/admintoolui/src/main/webapp/bower_components/angular/angular.js:4344:11 
    [email protected]:/Users/bgurung2/workspace/admintoolui/src/main/webapp/js/libs/angular-mocks.js:2797:44 
    [email protected]:/Users/bgurung2/workspace/admintoolui/src/main/webapp/js/libs/angular-mocks.js:2777:30 
    @c:/Users/bgurung2/workspace/admintoolui/src/main/webapp/js/tests/services/preferenceCategoryService.test.js:18:9 
    TypeError: $httpBackend is undefined in c:/Users/bgurung2/workspace/admintoolui/src/main/webapp/js/tests/services/preferenceCategoryServi 
ce.test.js (line 33) 
    @c:/Users/bgurung2/workspace/admintoolui/src/main/webapp/js/tests/services/preferenceCategoryService.test.js:33:9 
Firefox 38.0.0 (Windows 7 0.0.0): Executed 13 of 13 (1 FAILED) (0.016 secs/0.039 secs) 

我不知道爲什麼我有什麼錯。我試圖按照this stackoverflow question,但沒有線索。

任何幫助或建議將是偉大的!

這裏是我的應用程序:

'use strict'; 

/* App Module */ 

var app = angular.module('myApp', [ 
'ngRoute', // uri routing 
'controllers', // controllers 
'services', // services 
'ngMock' 

]); 

// module controllers 
var appCtrl = angular.module('controllers', []); 

// module services 
var appServices = angular.module('services', [ 'ngResource' ]); 

這裏是我的服務:

'use strict'; 

appServices.factory('MyCategoryService', ['$resource', 'REST_RESOURCE', 
function ($resource, REST_RESOURCE) { 
    return $resource(REST_RESOURCE.PREFERENCE, {}, { 
     query: { 
      method: 'GET', 
      params: {}, 
      isArray: false 
     } 
    }); 
}]); 

這裏是我的服務規範:

describe('service: MyCategoryService', function() { 
    var $httpBackend; 
    var $rootScope; 
    var MyCategoryService; 
    var REST_RESOURCE; 

    beforeEach(function() { 
     angular.mock.module('myApp'); 
     angular.mock.inject(function ($injector) { 
      $httpBackend = $injector.get('$httpBackend'); 
      $rootScope = $injector.get('$rootScope'); 
      MyCategoryService= $injector.get('MyCategoryService'); 
      REST_RESOURCE = $injector.get('REST_RESOURCE'); 
     }); 
    }); 

    it('should send a request to MyCategoryService', function() { 
     //TODO 
     var mockdata = { 
      items: { 
      } 
     }; 

     $httpBackend.expect('GET', REST_RESOURCE.PREFERENCE).respond(200, mockdata); 

     MyCategoryService.query(function (response) { 
      $rootScope.data = response.items; 
     }); 
     $httpBackend.flush(); 

     expect($rootScope.data).toEqual(mockdata); 
    }); 

}); 

這裏是我的karma.conf.js文件:

module.exports = function(config) { 
    config.set({ 

    // base path that will be used to resolve all patterns (eg. files, exclude) 
    basePath: '', 


    // frameworks to use 
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
    frameworks: ['jasmine'], 


    // list of files/patterns to load in the browser 
    files: [ 
     //dependencies 
     'bower_components/angular/angular.js', 
     'bower_components/angular-mocks/angular-mocks.js', 
     'bower_components/angular-route/angular-route.js', 
     'bower_components/angular-bootstrap/ui-bootstrap.js', 
     'bower_components/angular-bootstrap/ui-bootstrap-tpls.js', 
     'bower_components/angular-cookies/angular-cookies.js', 
     'js/app.js', 
     'js/libs/*.js', 
     'js/directives/*.js', 
     'js/routes.js', 
     'js/resource_uri.js', 
     'js/services/*.js', 
     'js/controllers/*.js', 
     //test files 
     'js/tests/*.test.js', 
     'js/tests/*/*.test.js' 
    ], 


    // list of files to exclude 
    exclude: [ 
    ], 


    // preprocess matching files before serving them to the browser 
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
    preprocessors: { 
    }, 


    // test results reporter to use 
    // possible values: 'dots', 'progress' 
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
    reporters: ['progress'], 


    // web server port 
    port: 9876, 


    // enable/disable colors in the output (reporters and logs) 
    colors: true, 


    // level of logging 
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
    logLevel: config.LOG_INFO, 


    // enable/disable watching file and executing tests whenever any file changes 
    autoWatch: true, 


    // start these browsers 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
    browsers: ['Firefox'], 


    // Continuous Integration mode 
    // if true, Karma captures browsers, runs the tests and exits 
    singleRun: false, 

    // Concurrency level 
    // how many browser should be started simultaneous 
    concurrency: Infinity 
    }); 
}; 
+0

您正在實例化模塊'admintool'在您的測試中,但我沒有看到在您的源代碼中的任何位置聲明此模塊? –

+0

對不起我的壞。在發佈問題時,我將admintool更改爲myApp。讓我做出這個改變。 –

+0

錯誤發生在'c:/ Users/bgurung2/workspace/admintoolui/src/main/webapp/js/tests/services/preferenceCategoryService.test.js'的第33行......哪行代碼是這樣的? –

回答

3

我經歷了一些文件中的一些更改後,終於使我的測試運行。我不知道具體的原因是什麼,但是我在下面的變化中得到了解決方案。這裏是我的變化如下:

我刪除了ngMocks依賴。更改後,我的app.js文件如下所示。

'use strict'; 

/* App Module */ 

var app = angular.module('admintool', [ 'ngRoute', // uri routing 
'controllers', // controllers 
'services', // services 
'angularUtils.directives.dirPagination', // pagination service 
'ui.bootstrap', // angular ui bootstrap 
'ui', // ui sortable 
'uiSwitch', // on of switch service 
'ngMessages', // for form validation 
'xeditable', // for table edit 
'ngCookies' 
// messages 
]); 

// module controllers 
var appCtrl = angular.module('controllers', []); 

// module services 
var appServices = angular.module('services', [ 'ngResource' ]); 

service.js樣子:

'use strict'; 

//appServices.factory('PreferenceCategory', ['$resource', 'REST_RESOURCE', 
angular.module('services').factory('PreferenceCategory', ['$resource', 'REST_RESOURCE', 
function ($resource, REST_RESOURCE) { 
    return $resource(REST_RESOURCE.PREFERENCE_CATEGORY, {}, { 
     query: { 
      method: 'GET', 
      params: {}, 
      isArray: false 
     } 
    }); 
}]); 

serviceSpec.js樣子:

describe('Preference Category Service', 
    function() { 

     beforeEach(angular.mock.module("admintool")); 

     var httpBackend, pc; 

     beforeEach(inject(function($httpBackend, PreferenceCategory) { 
      httpBackend = $httpBackend; 
      pc = PreferenceCategory; 
     })); 

     afterEach(function() { 
      httpBackend.verifyNoOutstandingExpectation(); 
      httpBackend.verifyNoOutstandingRequest(); 
     }); 

     it(
       'Check GET Request', 
       function() { 

        httpBackend 
          .expectGET(
            'http://jboss-pmadmin-tool-dev.ose-core.optum.com/pmadmin-tool/v1/preference_categories') 
          .respond({ 
           username : 'test' 
          }); 

        // call the function on our service instance 
        var response = pc.query(); 

        httpBackend.flush(); 

        expect(response.username).toEqual('test'); 
       }); 
    }); 

karma.conf.js文件,我刪除'js/libs/*'並添加所使用的單個文件在應用程序中。

module.exports = function(config) { 
    config.set({ 

     // base path that will be used to resolve all patterns (eg. files, 
     // exclude) 
     basePath : '../admintoolui/src/main/webapp/', 

     // frameworks to use 
     // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
     frameworks : [ 'jasmine' ], 

     // list of files/patterns to load in the browser 
     files : [ 
        'bower_components/angular/angular.min.js', 
        'bower_components/angular-mocks/angular-mocks.js', 
        'js/libs/angular-resource.js', 
        'bower_components/angular-route/angular-route.js', 
        'bower_components/angular-bootstrap/ui-bootstrap.js', 
        'js/libs/pagination.js', 
        'js/libs/angular-ui.js', 
        'js/libs/angular-ui-switch.min.js', 
        'js/libs/angular-messages.js', 
        'js/libs/xeditable.min.js', 
        'bower_components/angular-cookies/angular-cookies.js', 

        'js/app.js', 
        'js/resource_uri.js', 

        'js/services/*.js', 
        'js/controllers/*.js', 

        'js/tests/**/*.test.js' 

     ], 

     // list of files to exclude 
     exclude : [], 

     // preprocess matching files before serving them to the browser 
     // available preprocessors: 
     // https://npmjs.org/browse/keyword/karma-preprocessor 
     preprocessors : {}, 

     // test results reporter to use 
     // possible values: 'dots', 'progress' 
     // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
     reporters : [ 'progress' ], 

     // web server port 
     port : 9876, 

     // enable/disable colors in the output (reporters and logs) 
     colors : true, 

     // level of logging 
     // possible values: config.LOG_DISABLE || config.LOG_ERROR || 
     // config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
     logLevel : config.LOG_INFO, 

     // enable/disable watching file and executing tests whenever any file 
     // changes 
     autoWatch : true, 

     // start these browsers 
     // available browser launchers: 
     // https://npmjs.org/browse/keyword/karma-launcher 
     browsers : [ 'Firefox' ], 

     // Continuous Integration mode 
     // if true, Karma captures browsers, runs the tests and exits 
     singleRun : false, 

     // Concurrency level 
     // how many browser should be started simultaneous 
     concurrency : Infinity 
    }) 
}