0
我正在为调用Promise的作用域函数编写测试,并试图访问在承诺解析后设置的某些作用域属性。用Jasmine调用承诺的测试作用域函数
这基本上是我的控制器功能:
$scope.submitNewForm = function(){
Service.createUser($scope.user).then(function(result){
if (result){
$scope.message = 'success';
} else {
$scope.message = 'error';
}
$scope.$apply();
});
};
如果调用范围功能后,我的$scope.message
是设置好的我非常想测试。
到目前为止,我已经使用$q.defer()
一些测试,但对我缺少什么,我没有成功
beforeEach(inject(function ($controller, $rootScope, _Service_, $q) {
scope = $rootScope.$new();
Service= _Service_;
deferred = $q.defer();
UserController= $controller('UserController', {
$scope: scope
});
}));
it('should call Service.createUser and set a message', function(){
spyOn(Service, 'createUser').and.callFake(function() {
return deferred.promise;
});
scope.submitNewForm();
scope.$digest();
expect(scope.message).toBeDefined();
});
任何想法?
谢谢你的答案Siddharth。我现在得到一个'错误:[$ rootScope:inprog] $ digest已在进行中',这可能是因为我在我的Promise回调中调用'$ scope。$ apply()',当我摆脱它时它就起作用了。但我需要它来应用我认为的变化。有什么想法吗? – adolfosrs