【问题标题】:Test passes without 'beforeEach', but fails with injection没有“beforeEach”的测试通过,但注入失败
【发布时间】:2016-11-01 03:55:32
【问题描述】:

我创建了一个网络应用程序并尝试使用 Karma & Jasmine 测试我的 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
})
}

当我在没有从 //load 控制器模块到“it”案例的情况下运行我的测试时,它通过了。但是有了它,我的终端中出现错误和以下内容: 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

我不熟悉 angular、jasmine 和 karma,并试图通过其他问题搜索我的方式,但没有任何效果。有什么想法可以帮助吗?

【问题讨论】:

  • 看起来“./login/LoginController.js”的路径不正确。
  • 你如何声明你的登录模块?
  • var buboApp = angular.module('login', ['httpService','logService', 'vButton', 'ngNotify']);这是登录模块,后面跟着 buboApp.controller('LoginController', ...。我的 LoginController 位于目录根目录的登录文件夹中。这样正确吗?

标签: javascript angularjs jasmine karma-jasmine


【解决方案1】:

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

【讨论】:

  • 我的控制器依赖于我的两个服务和 v-button 和 ng notify。全部包含在 karma.conf.js.. 仍然错误。
  • 等等,httpServicelogService 实际上是服务还是模块?
  • 它们是一个 module().service... 我不太清楚这里有什么区别。它现在以某种方式工作,但我现在在访问 rootScope 变量时遇到问题。不过谢谢!
  • 您不能在模块的依赖项中指定实际的服务。模块只能依赖于其他模块。
猜你喜欢
  • 2020-08-08
  • 2016-05-17
  • 2017-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
相关资源
最近更新 更多