[英]Should I test calls to my service inside BootstrapDialog prompt?
我正在使用 BootstrapDialog 來顯示一個對話框。 如果用戶單擊刪除,它會調用我的服務並將其從數據庫中刪除。 如果他們單擊取消,則會關閉對話框。
我正在編寫單元測試,這讓我很困惑。 對我的服務的調用嵌套得很深,我什至不知道如何讓測試知道我正在測試的路徑。
我在控制器中的代碼:
$scope.deleteInventoryEntry = function(id){
//launch dialog
BootstrapDialog.show({
title: 'CONFIRM DELETION',
message: 'Are you sure you want to delete this record?',
closable: false,
type: BootstrapDialog.TYPE_DANGER,
buttons: [{
label: 'Cancel',
action: function(dialog) {
dialog.close();
}
}, {
label: 'Delete',
icon: 'glyphicon glyphicon-remove',
cssClass: 'btn-danger',
action: function(dialog) {
//remove item from database
tankService.deleteInventoryEntry(id).success(function (response) {
//remove item from table if successful
if(response > 0){
//figure out which item to remove from table
var pos = $scope.invTable.filtered.map(function(item) { return item._id; }).indexOf(id);
//remove from table
$scope.invTable.filtered.splice(pos,1);
$scope.selectedItem.lineItems = [];
dialog.close();
//$scope.successGrowl(' QC Deleted Successfully');
}
});
}
}
]
});
};
我的測試
it('prompts on delete inventory item', function(){
spyOn(BootstrapDialog, 'show').and.callThrough();
$scope.deleteInventoryEntry(1);
expect(BootstrapDialog.show).toHaveBeenCalled();
});
我還可以測試是否說 ID 是 NAN 或 Null 並且不應該顯示對話框。 但我只是好奇我是否應該以某種方式測試 tankService.deleteInventoryEntry() 是否被調用。 我覺得我應該這樣做,但這是否意味着我必須模擬整個對話框項?
任何幫助我指明正確方向的幫助將不勝感激。
謝謝!
任何測試的經驗法則。 不要測試實現,而是測試行為。 例如,您應該測試當您填寫表單並單擊提交按鈕時,它被發送到您的 API 並且發生了一些響應。 測試應該盡可能獨立於視圖部分(例如,表單是位於模態中還是頁面中的某處)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.