我想爲我的控制器運行單元測試用例,我試過以下,但得到下面的錯誤,請讓我知道成功運行此測試。提前致謝。未被捕獲的錯誤:不匹配的匿名定義()模塊:功能在業力和茉莉花單元測試與requirejs
spec.js:
describe('MainCtrl', function() {
beforeEach(module('app-module'));
var $controller;
beforeEach(inject(function(_$controller_){
$controller = _$controller_;
}));
describe('$scope.titleofapp', function() {
var $scope, controller;
beforeEach(function() {
$scope = {};
controller = $controller('MainCtrl', { $scope: $scope });
});
it('sets the title of app to "app-module" module', function() {
$scope.titleofapp();
expect($scope.title).toEqual('My Test App !');
});
});
});
app.js:
define(['angular', './app-module'], function(angular, appModule) {
'use strict';
return appModule.controller('MainCtrl', ['$http', '$q', 'appService', '$stateParams', function($http, $q, $stateParams, appService){
$scope.titleofapp = function(){
$scope.title = 'My Test App !';}
}]);
});
下面是我的karma.conf.js文件中的配置:
karma.conf.js:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine','requirejs'],
files: [
'node_modules/requirejs/require.js',
'node_modules/karma-requirejs/lib/adapter.js',
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'public/app/app.js',
'public/tests/spec.js'
],
exclude: [
],
plugins: [
'karma-requirejs',
'karma-chrome-launcher',
'karma-jasmine'
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
}
我已配置requirejs li柯以上,現在,提示以下錯誤: 錯誤:
Uncaught Error: Mismatched anonymous define() module: function (angular, appModule) {
'use strict';
return appModule.controller('MainCtrl', ['$http', '$q', 'appService', '$stateParams', function($http, $q, $stateParams, appService){
$scope.titleofapp = function(){
$scope.title = 'My Test App !';}
}]);
}
http://requirejs.org/docs/errors.html#mismatch
at node_modules/requirejs/require.js:165
看起來你正在使用'requireJS',但是你沒有將它包含在'files'數組中。 –
你看過[指導如何與require.js一起運行業力](http://karma-runner.github.io/1.0/plus/requirejs.html)?我不能談論角度的東西,但關於requirejs業力配置不恰當 – xmike
@xmike,我已經添加了requirejs並獲取錯誤:未捕獲的錯誤:不匹配的匿名定義()模塊:函數,並更新了帖子,請讓我知道關於它,預先感謝。 – Sana