我有一个Angular应用程序,其中包含简单的业力测试和使用requirejs和Angular 1.2.28的非常简单的配置。我的测试还可以。
/**
* Created by jose on 7/12/2015.
*/
/*global module, inject */
define(['home', 'angularMocks','ngStorage'], function(app) {
'use strict';
describe('homeController', function () {
var scope, $location, createController;
beforeEach(module('homeApp'));
beforeEach(inject(function ($rootScope, $controller, _$location_) {
$location = _$location_;
scope = $rootScope.$new();
createController = function () {
return $controller('homeController', {
'$scope': scope
});
};
}));
it('should have message', function () {
var controller = createController();
$location.path('/');
expect($location.path()).toBe('/');
expect(scope.message).toEqual('This is Add new order screen');
});
});
});
我的模块:
/**
* Created by jose on 7/12/2015.
*/
'use strict';
define(['angular', 'angularRoute'], function(angular) {
var app = angular.module('homeApp', ['ngRoute']);
app.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'partials/home.html',
controller: 'homeController'
})
.when('/404', {
templateUrl: 'partials/404.html',
})
.otherwise({
redirectTo: '/404'
});
}
]);
app.controller('homeController', function ($scope) {
$scope.message = 'This is Add new order screen';
});
return app;
});
不幸的是,当我尝试将ngStorage添加为此模块的依赖项时,它不再适用。即使尝试将ngStorage添加到我的业力配置中也会产生如下错误:
Error: Mismatched anonymous define() module: function (app) {
只有当我尝试使用ngStorage时才会发生这种情况,当我只是将它评论到我的karma.conf文件中时,错误消失并且一切正常...... 如果无法使用带有Karma的ngStorage,还有另一种替代方案可以用于ngStorage吗?感谢
karma.conf
// Karma configuration
// Generated on Mon Jul 13 2015 09:49:28 GMT-0300 (Hora est. Sudamérica Pacífico)
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', 'requirejs'],
// list of files / patterns to load in the browser
files: [
'test-main.js',
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-route/angular-route.js',
'bower_components/ngstorage/ngStorage.js',
{pattern: 'javascripts/*.js', included: false},
{pattern: 'test/**/*Spec.js', included: false}
],
// list of files to exclude
exclude: [
'javascripts/config.js'
],
// 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_DEBUG,
// 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: ['PhantomJS'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
项目位于github:https://github.com/jbarros35/node/tree/master/angulartemplate
亲切的问候。答案 0 :(得分:1)
看起来好像你并不是唯一一个在尝试同时使用Karma和ngStorage时遇到错误的人。 Karma的工作人员已经提供了一些代码来帮助解决问题。你可以在这里查看:
答案 1 :(得分:0)
如果您查看ngStorage
的代码,您会看到它调用define
。所以这是一个合适的AMD模块。您在Karma设置中使用的每个AMD模块都必须在included: False
中加载files
:
{ pattern: 'bower_components/ngstorage/ngStorage.js', included: False }
否则,Karma会使用script
元素加载它,您将收到错误。
答案 2 :(得分:-1)
这是因为require js的异步加载功能。错误可以通过这种方式解决
转到test-main.js。 以这种方式添加垫片
shim:{
'yourJsModule':{
deps:['angular','yourApp']
}
}
替换" yourJsModule"使用您想要测试的角度文件和“你的应用程序”。使用角度模块文件......