5
當運行一個茉莉單元測試用於角控制器,它失敗消息
'Error: 10 $digest() iterations reached. Aborting!'
當$ httpbackend.flush()被調用。
這是我的控制器:
theApp.controller("myCtrl", function($scope, $http, globalstate){
$scope.currentThing = globalstate.getCurrentThing();
$scope.success = false;
$scope.$watch(globalstate.getCurrentThing, function(newValue, oldValue){
$scope.currentThing = newValue;
});
$scope.submitStuff = function(thing){
$http.put('/api/thing/PutThing', thing, {params: {id: thing.Id}})
.success(function(){
$scope.success = true;
})
};
});
這是我的單元測試:
describe('myCtrl', function(){
var myController = null;
beforeEach(angular.mock.module('theApp'));
beforeEach(inject(function($injector){
$rootScope = $injector.get('$rootScope');
scope = $rootScope.$new();
$httpBackend = $injector.get('$httpBackend');
$controllerService = $injector.get('$controller');
mockGlobalState = {
getCurrentThing : function(){
return {Id: 1, name: 'thing1'};
}
};
$controllerService('myCtrl', {$scope: scope, globalstate: mockGlobalState});
}));
it('should set flag on success', function(){
var theThing = {Id: 2, name: ""};
$httpBackend.expectPUT('/api/thing/PutThing?id=2',JSON.stringify(theThing)).respond(200,'');
scope.submitStuff(theThing, 0);
$httpBackend.flush();
expect(scope.basicupdateSucceeded).toBe(true);
});
});
當我將$ scope。$ watch中的第三個參數設置爲true(比較對象相等性而不是引用)時,測試通過。
爲什麼$ httpbackend.flush()會導致$ watch觸發? 爲什麼手錶在這之後觸發自己?
'submitVenue'定義在哪裏? – zsong
它應該是submitStuff()。該功能在控制器中定義。謝謝。相應地更改了問題。 – Torleif
看看這個,這可能有幫助。這與你的測試無關。 http://stackoverflow.com/questions/13594732/maxing-out-on-digest-iterations?rq=1 – zsong