我是Karma和Jasmine的新手,我用它为AngularJs应用程序编写单元测试。
在运行测试套件时,默认情况下会调用我调用的函数来初始化控制器中的数据。有没有办法在运行测试套件时避免这个调用?
找到下面的代码段:
BaseCtrl.js
App.controller('BaseCtrl', ['$scope', '$rootScope', 'Data', function($scope, $rootScope, Data){
//initialize
$rootScope.loader = false;
$rootScope.data = false;
$scope.init = function(){
$rootScope.loader = true;
Data.init(function(data){
$rootScope.loader = false;
$rootScope.data = data;
});
};
$scope.init();
}])
Data.js
App.service('Data', ['$http', function($http){
return {
init:function(callback){
$http.get('/url').success(function(response){
callback(response);
});
}
};
}]);
BaseCtrl.spec.js
describe('BaseCtrl', function(){
beforeEach(module('app'));
var $controller, $rootScope, $scope;
beforeEach(inject(function(_$controller_, _$rootScope_){
$controller = _$controller_;
$rootScope = _$rootScope_.$new();
}));
describe('Loader', function(){
var controller, $scope = {};
beforeEach(function(){
controller = $controller('BaseCtrl', {$scope:$scope, $rootScope:$rootScope});
});
it('should be false by default', function(){
// Check the default value
expect($rootScope.loader).toBe(false);
// Some code to determine the call, then
expect($rootScope.loader).toBe(true);
// Some code to determine the call is done, then
expect($rootScope.loader).toBe(false);
});
});
});
答案 0 :(得分:0)
是的,使用minHeight
创建模拟并返回您想要的任何内容。
$httpBackend