没有' beforeEach'的测试通过,但是注入失败

时间:2016-10-31 12:38:15

标签: javascript angularjs jasmine karma-jasmine

我创建了一个网络应用程序并尝试使用Karma& amp;来测试我的JavaScript茉莉花。我的测试看起来像这样:

'use strict';

describe('Controller: LoginController', function () {

   // load the controller's module
   beforeEach(module('login'));

   var MainCtrl,
       scope;

   // Initialize the controller and a mock scope
   beforeEach(inject(function ($controller, $rootScope) {
       scope = $rootScope.$new();
       MainCtrl = $controller('LoginController', {
           $scope: scope
       });
   }));

   it('true is of course true', function () {
       expect(true).toBe(true);
   });
});

我将以下内容放在我的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: [
  './node_modules/angular/angular.js',
  './node_modules/angular-mocks/angular-mocks.js',
  './node_modules/angular-route/angular-route.js',
  './app.js',
  './login/LoginController.js',
  './tests/*.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: ['Safari'],

plugins: [
  'karma-jasmine',
    'karma-safari-launcher'
],


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

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

当我在没有部件的情况下运行我的测试//将控制器的模块加载到"它"案件,它通过。但有了它我在我的终端中得到一个错误和以下内容: node_modules/angular/angular.js:68:32 node_modules/angular/angular.js:4640:30 forEach@node_modules/angular/angular.js:321:24 loadModules@node_modules/angular/angular.js:4601:12 createInjector@node_modules/angular/angular.js:4523:30 workFn@node_modules/angular-mocks/angular-mocks.js:3074:60 loaded@http://localhost:9877/context.js:151:17

我是角色,茉莉和业力的新手,并试图通过其他问题搜索我的方式,但没有任何作用。任何想法都可能有所帮助?

1 个答案:

答案 0 :(得分:0)

好吧,我没有看到你告诉业力包含你的登录模块所依赖的模块。这可能是当angular尝试在beforeEach中初始化模块时测试失败的原因。