我正在尝试测试控制器功能,但是Karma无法按预期接收它。我遵循了https://docs.angularjs.org/guide/controller
的指示这是我的代码:
var app = angular.module('calApp', []);
app.controller('calCtrl', function($scope) {
$scope.sum = function(x, y) {
return x + y;
};
});
describe('calCtrl function', function() {
describe('calCtrl', function() {
var $scope;
beforeEach(module('calApp'));
beforeEach(inject(function($rootScope, $controller) {
$scope = $rootScope.new();
$controller('calCtrl', {$scope: $scope});
}));
it('should add 2 numbers correctly', function() {
expect($scope.sum(1, 2)).toBe(3);
});
it('should subtract 2 numbers correctly', function() {
expect($scope.subtract(5, 3)).toBe(2);
});
});
});
我希望测试为$ scope.sum()提供1次传递,为$ scope.substract()提供1次失败。请帮忙!
错误: TypeError:undefined不是test / spec.js中的对象(评估'$ scope.subtract')
此外: 执行2 of 2(2 FAILED)
karma.conf.json:
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: [
'lib/angular.js',
'lib/angular.min.js',
'lib/angular-mocks.js',
'public_html/*.html',
'public_html/*.js',
'tests/*.js'
],
// list of files to exclude
exclude: [
"**/angular-scenario.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_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: ['PhantomJS'],
// 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
})
}
更新:我也有一个失败的期望(真实).toBe(true)
答案 0 :(得分:0)
您没有定义subtract
。
定义subtract
app.controller('calCtrl', function($scope) {
$scope.subtract= function(x, y) {
return x - y;
};
$scope.sum = function(x, y) {
return x + y;
};
});
或者您可以按以下方式编写减法: -
it('should subtract 2 numbers correctly', function() {
expect($scope.subtract).toBeDefined();
expect($scope.subtract(5, 3)).toBe(2);
});
根据错误更新:
您的beforeEach有问题
var $controller;
beforeEach(inject(function(_$controller_) {
$controller = _$controller_;
}));
然后在您的测试中进一步创建:
it('should add 2 numbers correctly', function() {
var $scope = {};
var controller = $controller('calcCtrl', {$scope:$scope});
expect($scope.sum(1, 2)).toBe(3);
});