利用 jasmine + karma编写angularjs的controller单元测试用例 一直报错
我利用 jasmine + karma编写angularjs的controller单元测试用例,一直报错:Error: [ng:areq] Argument 'roleCtrl' is not a function, got undefined,
大伙帮忙看看啥问题?
源码如下:
(1):roleCtrl
import ngApp from '../components/app';
export default ngApp.controller('roleCtrl', ['$scope', function($scope) {
$scope.columns = [
{ "title": "角色", "attribute": "roleName", "type": "data" },
{ "title": "权限说明", "attribute": "description", "type": "data" }
];
$scope.url = "/api/role/search/find";
$scope.catalog = "role";
$scope.param = { "systemCode": localStorage.syscode, page: 0, size: 10, isDelete: false };
}]);
(2):spec.js
'use strict';
angular.module('ngApp', ['ui.router'])
.value('rolefactory', 'rolefactory')
.value('dialogService', 'dialogService');
describe("ngApp Unit Test", function() {
beforeEach(angular.mock.module("ngApp"));
var scope, ctrl;
beforeEach(inject(function($controller, $rootScope) {
//模拟生成scope, $rootScope是angular中的顶级scope,angular中每个controller中的
//scope都是rootScope new出来的
scope = $rootScope.$new();
//模拟生成controller 并把先前生成的scope传入以方便测试
ctrl = $controller('roleCtrl', { $scope: scope });
}));
// controller test
describe('role controller test', function() {
it('Add Cat Controller test', function() {
expect(50).toBe(50);
})
})
});
(3):karma配置
// Karma configuration
// Generated on Tue Mar 21 2017 12:39:40 GMT+0800 (CST)
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-ui-router/release/angular-ui-router.js',
'node_modules/angular-mocks/angular-mocks.js',
// 'app/components/*.js',
// 'app/controller/*.js',
// 'app/components/**/*.js',
'app/controller/rolectrl.js',
'unit/controller/*.js'
],
// list of files to exclude
exclude: ['karma.conf.js'],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'app/controller/rolectrl.js': ['webpack', 'coverage']
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'coverage'],
// 设置输出测试内容文件的信息
// junitReporter: {
// outputFile: 'test_out/unit.xml',
// suite: 'unit'
// },
// 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,
// plugins: [
// 'karma-chrome-launcher',
// 'karma-firefox-launcher',
// 'karma-jasmine',
// 'karma-junit-reporter'
// ],
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
//webpack: require('./webpack.config')(true)
webpack: {
module: {
loaders: [{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/,
query: {
presets: ['es2015']
}
}]
//plugins: ['node_modules/angular/angular.js', 'node_modules/angular-ui-router/release/angular-ui-router.js', 'node_modules/angular-mocks/angular-mocks.js']
}
}
})
}
错误截图:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没人遇到这种问题么?
谢谢邀请,大概看了看,可能是
spec.js
里面没有roleCtrl
声明的代码吧,你加上试试?这方面经验较少,我表示我一直没有使用过ng中的$mock服务写一些单元测试,不是不想写,是真没有时间写。