我有以下功能,我想窥探...但它包含一个承诺...但我得到TypeError:' undefined'不是对象(评估' modalService.showModal({},modalOptions)。然后')
因为我当然只是spyOn(modalService,' showModal')
我如何解释承诺呢?
_modalService = {
close: function (value) { console.log(value) },
dismiss: function (value) { console.log(value) },
showModal: function (value) { console.log(value) }
};
spyOn(_modalService, 'close');
spyOn(_modalService, 'dismiss');
spyOn(_modalService, 'showModal');
控制器功能:
user.resetPassword = function () {
var modalOptions = {
closeButtonText: 'Cancel',
actionButtonText: 'Reset',
headerText: 'Reset Password',
bodyText: 'Are you sure you want to reset the users password?'
};
modalService.showModal({}, modalOptions).then(function (result) {
if (result === 'ok') {
userDataService.resetPassword(user.data).then(function (result) {
$scope.$emit('showSuccessReset');
});
};
});
};
这是我的单元测试:
it('should allow the users password to be reset', function () {
var controller = createController();
controller.resetPassword();
$httpBackend.flush();
})
******************* UPDATE
所以我把它改成了这个:
//Create a fake instance of the modal instance. TO ensure that the close is called
_modalService = {
close: function (value) { console.log(value) },
dismiss: function (value) { console.log(value) },
showModal: function (value) { console.log(value) }
};
spyOn(_modalService, 'close');
spyOn(_modalService, 'dismiss');
spyOn(_modalService, 'showModal').and.callThrough();
_modalService.showModal = function() {
var deferred = $q.defer();
deferred.resolve('Remote call result');
return deferred.promise;
};
说实话,虽然我不确定我能否解释一下。虽然我理解所有异步的东西......我不确定茉莉是如何使用它来使它全部工作的。任何人都可以解释流量????另外我觉得语法错了......你通常会怎么写这个看起来更好/更清洁...... ??
答案 0 :(得分:4)
当你需要模拟一个返回一个promise的函数时,你有两个选择:
我建议#2因为它更容易,你不必担心复制整个promise API。换句话说,不值得嘲笑承诺本身。
现在关于Jasmine:当你已经拥有一个对象(不是模拟)时,你只需要使用spyOn
,并且你想要窥探其中一个方法(没有双关语)。在您的情况下,您的整个对象都是假的,因此您可以改为使用jasmine.createSpyObj
。
以下示例应使上述所有内容更加清晰:
<强> SUT 强>
app.controller('MainCtrl', function($scope, modal, service) {
$scope.click = function() {
modal.show().then(function(result) {
if (result === 'ok') {
service.resetPassword();
}
});
};
});
<强>测试强>
describe('Testing a controller', function() {
var $scope, $q,
ctrl, modalMock, serviceMock;
beforeEach(function() {
module('plunker');
modalMock = jasmine.createSpyObj('modal', ['show']);
serviceMock = jasmine.createSpyObj('service', ['resetPassword']);
inject(function($rootScope, $controller, _$q_) {
$scope = $rootScope.$new();
$q = _$q_;
ctrl = $controller('MainCtrl', {
$scope: $scope,
modal: modalMock,
service: serviceMock
});
});
});
it('should reset the password when the user confirms', function() {
// Arrange
var deferred = $q.defer();
deferred.resolve('ok');
modalMock.show.and.returnValue(deferred.promise);
// Act
$scope.click();
$scope.$digest(); // Makes Angular resolve the promise
// Assert
expect(serviceMock.resetPassword).toHaveBeenCalled();
});
it('should not reset the password when the user cancels', function() {
// Arrange
var deferred = $q.defer();
deferred.resolve('cancel');
modalMock.show.and.returnValue(deferred.promise);
// Act
$scope.click();
$scope.$digest(); // Makes Angular resolve the promise
// Assert
expect(serviceMock.resetPassword).not.toHaveBeenCalled();
});
});
每个测试中的模拟排列代码可以移动到beforeEach
部分,因此不会重复。为了简单起见,我没有这样做。