[英]AngularJs Unit Testing Controller with sweetalert in Jasmine Testing
我正在尝试使用 jasmine 和 karma 测试我的 angularjs 控制器,但我无法测试处于 Sweetalert 函数中的代码块。 我如何从我的测试类中确认测试 $scope.getCategory() 的甜蜜函数是否被调用? 这是我的控制器和茉莉花测试用例中的示例代码。
控制器:
$scope.changeCategoryStatus = function(selectedId, selectedActive, topId) {
sweet.show({
title : "Kategoriyi "+(!selectedActive ? 'aktif' : 'pasif')+ " hale getirmek istiyor musunuz?",
type : "warning",
showCancelButton : true,
confirmButtonColor : "#DD6B55",
confirmButtonText : "Evet, değiştir!",
closeOnConfirm : false,
showLoaderOnConfirm : true,
html : false
}, function() {
$http({
method : "POST",
url : webRootUrl+"ajax/category/setActivation",
data : {
"id" : selectedId,
"active" : !selectedActive
}
}).then(function(response) {
//console.log(JSON.stringify(response.data))
if(response.data.outPutDouble==-7){
swal("Değiştirilemedi!", response.data.outPutString,"error");
}else{
$scope.getCategory(topId);
swal("Bu kategorinin durumu değiştirildi","",
"success");
}
}, function myError(response) {
swal("Bu kategorinin durumu değiştirilemedi","","error");
//console.log("***aaa" + JSON.stringify(response))
});
});
}
茉莉花测试用例:
it("changeCategoryStatus success", function () {
$scope.changeCategoryStatus(21,true,0)
spyOn($scope,'getCategory')
expect($scope.getCategory).toHaveBeenCalled(); });
有没有人之前遇到过类似的问题? 预先感谢您的帮助。
这是我在 Angular 2 应用程序单元测试中所做的
it("should delete token", () => {
spyOn(localStorage, 'removeItem');
// call function
comp.deleteToken();
// add some delay till the sweetAlert modal show up
setTimeout(() => {
// select the button for the activity you want to test
let cancelSwal: HTMLButtonElement = fixture.debugElement.query(By.css(".sa-button-container > .cancel")).nativeElement;
cancelSwal.click();
// test your sweetAlert callback function
expect(localStorage.removeItem).toHaveBeenCalledWith("token");
}, 100);
});
正如你所看到的,它直截了当,没什么特别的,
setTimeout
添加延迟以确保存在 swal 模式click
事件)以获取要测试的回调函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.