4
我的代碼是:我如何測試一個被Karma嘲笑的承諾?
$scope.openModal = openModal;
function openModal(data){
var info = data || {}
ModalService.showModal({
templateUrl: ENVApp+"/myview.html",
controller: "ModalController",
inputs: {
icon : "",
title: "Additional",
data : info
}
}).then(function (modal) {
modal.element.modal();
modal.close.then(function (res) {
if(res.data != 'cancel'){
if(!res.data.id){
create(res)
}else{
update(res)
}
}
});
});
}
什麼是與噶對此進行測試的正確方法?這是我到目前爲止有:
在我beforeEach
:
this.ModalService = {
showModal: function(obj) {
var deferred = $q.defer();
deferred.resolve({
element: {
modal: jasmine.createSpy(),
close: function() {
var deferred2 = $q.defer();
return deferred2.promise;
}
}
});
return deferred.promise;
}
};
this.UserService = {
user: {
id: 2
},
updateCategory: function(data, type) {
var deferred = $q.defer();
deferred.resolve({
data: {
rows: 'response'
}
});
return deferred.promise;
}
}
this.controller = $controller('ProfileAdditionalController', {
'$scope': this.scope,
'$rootScope': this.rootScope,
'$state': this.state,
"$stateParams": this.stateParams,
'ModalService': this.ModalService,
'UserService': this.UserService,
'ResourceService': this.ResourceService,
'$upload': this.upload,
'userData': this.userData
});
對於我的實際測試:
it('should open a modal when requested', function() {
this.scope.openModal();
expect(this.ModalService.showModal)
});
但是,這並不讓一大堆的道理。思考?
你有沒有'openModal'返回'showModal'使用的諾言。我最近正在測試一個GUI小部件,其功能類似於您的代碼所做的事情:方法A調用方法B,並且B返回A正在使用的承諾。我只是決定讓A回報它從B(+處理程序)得到的承諾。它大大簡化了測試。 – Louis