[英]How do I do a sinon stub to replace the promise so that the response is mocked?
我希望整個諾言都被扼殺。
我正在使用帶有 javascript、jasemine 和 karma 的 angular js 版本 1。
這是我的代碼:
appcontrolmod.controller('loginCtrl', ['$rootScope', '$scope', 'personService',
function ($rootScope, $scope personService) {
$scope.getUser= function(){
//I want to stub this to return an expected response?
personService.getUser().then((response) => {
//I want to stub the response of this promise?
console.log(response);
if(response){
//update model
}else{
//show error message
}
});
}
$scope.getUser();
}]);
提前致謝!
我會說那個地方監視方法,然后從那個間諜那里返回一些假數據
//place spy over it & return fakedata, it should be in global before each
var getUserSpy = spyOn(personService, 'getUser').and.callFake(function() {
return {
then: function(callback) { return callback({Id: 1, Name: 'Something'}); }
};
});
it('should show success when modal login returns success response', function() {
scope.getUser();
//do assert by calling expect statement
expect(personService.getUser).toHaveBeenCalled();
expect(scope.user.Id).toBe(1); //assuming scope.user has filled with response user data
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.