gpt4 book ai didi

javascript - Angular JS 驳回 $modalInstance : undefined is not a function

转载 作者:行者123 更新时间:2023-12-03 11:18:29 27 4
gpt4 key购买 nike

编写一个测试来测试我的应用程序的删除功能。我创建了一个模拟删除 $modal 来模拟取消/确认删除。

  var modalInstanceMock=
{
result: {
then: function(confirmCallback, cancelCallback) {
//Store the callbacks for later when the user clicks on the OK or Cancel button of the dialog
this.confirmCallBack = confirmCallback;
this.cancelCallback = cancelCallback;
}
},
confirmCallBack: function(item){
return true;
},
cancelCallback: function(type){
return false;
},
close: function( item ) {
//The user clicked OK on the modal dialog, call the stored confirm callback with the selected item

this.result.confirmCallBack( item );
},
dismiss: function( type ) {
//The user clicked cancel on the modal dialog, call the stored cancel callback

this.result.cancelCallback( type );
}



};

我在每次测试之前都会这样做:

	beforeEach(inject(function($modal) {
spyOn($modal, 'open').andReturn(modalInstanceMock);
}));

这非常有效:

	var newRes = scope.deleteCar(car);
scope.modalInstance.close("ok");

但是当我尝试这个时:

var newRes = scope.deleteCar(car);
scope.modalInstance.dismiss("ok");

我收到一个 Type:error undefined is not a function at Object.modalInstanceMock.dismiss。

无法理解当关闭工作正常时出了什么问题。

最佳答案

在beforeEach中初始化,

modalInstance = {
close: jasmine.createSpy('modalInstance.close'),
dismiss: jasmine.createSpy('modalInstance.dismiss')
},

然后期待。

关于javascript - Angular JS 驳回 $modalInstance : undefined is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27226438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com