编写测试以测试我的应用程序的删除功能。我创建了一个模拟删除$ 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");

我得到一个类型:错误undefined不是Object.modalInstanceMock.dismiss中的函数。
当关闭工作正常时,无法理解出现了什么问题。
答案 0 :(得分:1)
在beforeEach中初始化,
modalInstance = {
close: jasmine.createSpy('modalInstance.close'),
dismiss: jasmine.createSpy('modalInstance.dismiss')
},
然后期待。