【发布时间】: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