在angularjs 1.5中运行karma来测试控制器时遇到问题。一切都还可以,但是当我将ui-router
注入我的模块时,它将被破坏。
我在文件 buffalo.template.module.js 中创建模块,如下所示
(function () {
'use strict';
angular
.module('Buffalo.template', [])
.config(['$stateProvider', routeConfig]);
/** @ngInject */
function routeConfig($stateProvider) {
$stateProvider
.state('crawler_template', {
url: '/crawler-template',
templateUrl: 'app/pages/buffalo/src/template/buffalo.template.html',
title: 'Crawler Template',
controller: "BuffaloTemplateController",
controllerAs: "tempCtrl",
sidebarMeta: {
icon: 'ion-ios-paper-outline',
order: 1
}
});
}
})();
文件中的控制器 BuffaloTemplateController.js
(function() {
'use strict';
angular.module('Buffalo.template')
.controller('BuffaloTemplateController', buffaloTemplateController);
/** @ngInject */
function buffaloTemplateController() {
var ctrler = this;
ctrler.name = 'test';
ctrler.base_url = undefined;
ctrler.list_urls = [undefined];
ctrler.addUrlLink = function () {
ctrler.list_urls.push(undefined);
};
ctrler.generate = function () {
console.log(ctrler.name);
console.log(ctrler.base_url);
};
}
})();
我的测试文件 BuffaloTemplateSpec.js
describe('Buffalo Template Module', function() {
describe('Controller', function() {
var controller;
beforeEach(function() {
module('Buffalo.template');
inject(function($controller) {
controller = $controller('BuffaloTemplateController');
})
});
it('should initialize the title in the scope', function() {
console.log(controller);
});
});
});
当业力运行我的测试时,失败消息
PhantomJS 2.1.1 (Linux 0.0.0) Buffalo Template Module Controller should initialize the title in the scope FAILED
forEach@/home/quangtm-s3/workspaces/personal/rtm/owl/bower_components/angular/angular.js:321:24
loadModules@/home/quangtm-s3/workspaces/personal/rtm/owl/bower_components/angular/angular.js:4601:12
createInjector@/home/quangtm-s3/workspaces/personal/rtm/owl/bower_components/angular/angular.js:4523:30
workFn@/home/quangtm-s3/workspaces/personal/rtm/owl/bower_components/angular-mocks/angular-mocks.js:3074:60
inject@/home/quangtm-s3/workspaces/personal/rtm/owl/bower_components/angular-mocks/angular-mocks.js:3054:46
unit/BuffaloTemplateSpec.js:11:12
loaded@http://localhost:9876/context.js:151:17
/home/quangtm-s3/workspaces/personal/rtm/owl/bower_components/angular/angular.js:4641:53
PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.04 secs / 0.008 secs)
但是当我评论模块配置时,业力运行测试成功。我的情况有什么问题。如何测试我的控制器,模块。谢谢你。