茉莉花测试新手。很抱歉,如果这是一个愚蠢的问题。我试图在AngularJS代码中对控制器的存在进行简单的单元测试,我无法克服这个错误。这是愚蠢的我确定。我已经遍布stackoverflow,并根据类似的错误尝试了许多不同的东西,但无济于事。继续得到错误。
这是app.js
angular.module('waldo', ['ui.router'])
.config(['$stateProvider','$urlRouterProvider',function($stateProvider,$urlRouterProvider){
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/home.html',
controller: 'MainController'
})
.state('posts', {
url: '/posts/{id}',
templateUrl: '/posts.html',
controller: 'PostsController'
});
$urlRouterProvider.otherwise('home');
}])
.factory('posts',[function(){
var o = {
posts: []
};
return o;
}])
.controller('MainController', [
'$scope','posts',
function($scope,posts){
$scope.posts = posts.posts;
$scope.addPost = function(){
if(!$scope.title || $scope.title === '') { return; }
$scope.posts.push({
title: $scope.title,
link: $scope.link,
upvotes: 0,
comments: [
{author: 'Joe', body: 'Cool post!', upvotes: 0},
{author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0}
]
});
$scope.title = '';
};
$scope.incrementUpvotes = function(post) {
post.upvotes += 1;
};
}])
.controller('PostsController',['$scope','$stateParams','posts',function($scope,$stateParams,posts){
$scope.post = posts.posts[$stateParams.id];
}]);
测试:
describe('test that', function() {
beforeEach(module('waldo'));
describe('MainController', function () {
var scope, createController;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
createController = function () {
return $controller('MainController', {
'$scope': scope
});
};
}));
it('exists', function () {
var controller = createController();
expect(controller).not.toBeNull();
});
});
});
karma.conf.js
// Karma configuration
// Generated on Wed Apr 08 2015 13:46:46 GMT-0400 (EDT)
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: [
"vendor/assets/bower_components/angular/angular.js",
"vendor/assets/bower_components/angular-mocks/angular-mocks.js",
"app.js",
"test/*.js",
"test/*/*_test.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: ['Firefox'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
bower.json
{
"name": "waldo",
"version": "0.0.0",
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"angular": "~1.3.15",
"angular-ui-router": "~0.2.13",
"bootstrap": "~3.3.4"
},
"devDependencies": {
"angular-mocks": "~1.3.15"
}
}
错误是:
minErr/<@/home/christian/Projects/waldo/vendor/assets/bower_components/angular/angular.js:63:12
loadModules/<@/home/christian/Projects/waldo/vendor/assets/bower_components/angular/angular.js:4138:15
forEach@/home/christian/Projects/waldo/vendor/assets/bower_components/angular/angular.js:323:11
loadModules@/home/christian/Projects/waldo/vendor/assets/bower_components/angular/angular.js:4099:5
createInjector@/home/christian/Projects/waldo/vendor/assets/bower_components/angular/angular.js:4025:11
workFn@/home/christian/Projects/waldo/vendor/assets/bower_components/angular-mocks/angular-mocks.js:2425:44
TypeError: createController is not a function in /home/christian/Projects/waldo/test/integration/first_test.js (line 17)
@/home/christian/Projects/waldo/test/integration/first_test.js:17:30
感谢。
答案 0 :(得分:1)
也许您必须将模块依赖项放在karma.conf.js中,在您的情况下是'ui.router',例如:
...
files: [
"vendor/assets/bower_components/angular/angular.js",
"vendor/assets/bower_components/angular-mocks/angular-mocks.js",
"vendor/assets/bower_components/angular-ui-router/angular-ui-router.js",
"app.js",
"test/*.js",
"test/*/*_test.js"
],
...
然后再次运行测试。
另外看看这些链接,它们对我非常有用