$ rootScope.isLoading在MainController中定义。我正在测试UsersController,在我调用服务以从API获取用户之后,我使用了isLoading函数。
users.test.js
'use strict';
describe('Users Controller', function () {
var $controller,
$scope;
beforeEach(module('app'));
beforeEach(inject(function (_$controller_) {
$controller = _$controller_;
}));
beforeEach(inject(function($rootScope){
$scope = $rootScope.$new();
controller = $controller('UsersController', { $scope: $scope });
}));
describe('$scope.usersList', function() {
it('should get first 20 users', function () {
expect($scope.usersList.length).toBe(20);
});
});
});
karma.conf.js
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: [
'bower_components/jquery/dist/jquery.js',
'bower_components/angular/angular.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-messages/angular-messages.js',
'bower_components/angular-ui-router/release/angular-ui-router.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'bower_components/angular-touch/angular-touch.js',
'bower_components/angular-cache/dist/angular-cache.js',
'bower_components/angular-mocks/angular-mocks.js',
'app/**/*.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: ['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
})
}
当我运行测试时,我得到 TypeError:$ rootScope.loading不是一个函数(我也得到0而不是预期的20但是稍后我会先解决这个问题)。< / p>
我也在$ rootScope。$ broadcast等类似的方法上得到了这个错误......
感谢任何帮助,谢谢!