[英]AngularJs Unit Testing Controller with sweetalert in Jasmine Testing
I'm trying to test my angularjs controller with jasmine and karma but i cant test codeblocks that are in sweetalert function.我正在尝试使用 jasmine 和 karma 测试我的 angularjs 控制器,但我无法测试处于 Sweetalert 函数中的代码块。 How can i confirm sweet function from my test class to test $scope.getCategory() is called or not?
我如何从我的测试类中确认测试 $scope.getCategory() 的甜蜜函数是否被调用? Here is a example code from my controller and jasmine test case.
这是我的控制器和茉莉花测试用例中的示例代码。
Controller:控制器:
$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))
});
});
}
Jasmine Test Case:茉莉花测试用例:
it("changeCategoryStatus success", function () {
$scope.changeCategoryStatus(21,true,0)
spyOn($scope,'getCategory')
expect($scope.getCategory).toHaveBeenCalled(); });
Has anyone had a similar problem earlier?有没有人之前遇到过类似的问题? Thanks in advance for help.
预先感谢您的帮助。
Here's what I did with my Angular 2 app unit test这是我在 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);
});
As you can see it's straight forward nothing fancy,正如你所看到的,它直截了当,没什么特别的,
setTimeout
to make sure the swal modal is theresetTimeout
添加延迟以确保存在 swal 模式click
event) to get to your call back function you want to testclick
事件)以获取要测试的回调函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.