如何在角度中包含模块?

时间:2015-12-09 07:15:12

标签: angularjs angularjs-directive jasmine karma-runner karma-jasmine

enter image description here我正在尝试使用jasmine进行测试。当我运行此命令 karma start karma.conf.js 时,它会给我这个错误。

Chrome 47.0.2526 (Windows 7 0.0.0) Firstcontroller Initialization title should be naveen FAILED
        Error: [$injector:modulerr] Failed to instantiate module app due to:
        Error: [$injector:nomod] Module 'app' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the depend
encies as the second argument.
        http://errors.angularjs.org/1.4.8/$injector/nomod?p0=app
            at D:/Webapplication/karmaTest/bower_components/angular/angular.js:68:12
            at D:/Webapplication/karmaTest/bower_components/angular/angular.js:2005:17
            at ensure (D:/Webapplication/karmaTest/bower_components/angular/angular.js:1929:38)
            at module (D:/Webapplication/karmaTest/bower_components/angular/angular.js:2003:14)
            at D:/Webapplication/karmaTest/bower_components/angular/angular.js:4435:22
            at forEach (D:/Webapplication/karmaTest/bower_components/angular/angular.js:340:20)
            at loadModules (D:/Webapplication/karmaTest/bower_components/angular/angular.js:4419:5)
            at Object.createInjector [as injector] (D:/Webapplication/karmaTest/bower_components/angular/angular.js:4344:11)
            at Object.workFn (D:/Webapplication/karmaTest/bower_components/angular-mocks/angular-mocks.js:2428:52)
            at window.inject.angular.mock.inject (D:/Webapplication/karmaTest/bower_components/angular-mocks/angular-mocks.js:2411:37)

我安装karma,karma-jasmine,karma-chrome启动器..当我运行像 expect(true).toBeTruthy 之类的简单测试时,它运行正常。但是当我尝试测试我的控制器时。它给了我上面的错误。

这是我的控制器

//(function () {
//    'use strict';
//
//    angular.module('app',[]).controller('FirstController',FirstController);
//
//    FirstController.$inject=['$scope']
//
//    function FirstController($scope){
//        $scope.title='naveen';
//    }
//
//})();

angular.module('app',[]).controller('FirstController',FirstController);

FirstController.$inject=['$scope'];

function FirstController($scope){
    $scope.title='naveen';
} 

这是我的 test-spec.js文件

describe('Firstcontroller', function () {
    var $rootScope,
        $scope,
        controller;

    beforeEach(function () {
         module('app');
        inject(function ($injector) {
            $rootScope = $injector.get('$rootScope');
            $scope=$rootScope.$new();
            console.log('==========================')
            controller=$injector.get('$controller')('FirstController',{$scope:$scope});
        })
    })

   describe('Initialization',function(){
       it('title should be naveen',function(){
         expect($scope.title).toEqual('naveen')
       })

   })
})

它给出了上面的错误原因? 我在karma.config文件中包含了包

karma.config文件

// Karma configuration
// Generated on Tue Dec 08 2015 18:55:16 GMT+0530 (India Standard Time)

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: [
        'bower_components/angular/angular.js',
      'bower_components/angular-mocks/angular-mocks.js',
      'bower_components/angular-resource/angular-resource.js',

      'app/**/.js',
      '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: ['Chrome'],


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

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

更新 当我将 'app/**/.js' 更改为'app / *时,我将js文件插入应用目录中.js'它工作正常..为什么有效?

1 个答案:

答案 0 :(得分:0)

 Module 'app' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

Angular代码中的模块为'apps'而不是'app'。 解决拼写错误,它应该有效。