我有一个包含以下方法的Angular服务:
function ChangeEvent(){
var calendarId = 'primary';
var eventId = 'omv6###########e8jbs';
var event = Calendar.Events.get(calendarId, eventId);
Logger.log('old recurrence = '+event.recurrence);
event.recurrence = '';
Calendar.Events.patch(event,calendarId,eventId);
Logger.log('new recurrence = '+event.recurrence);
}
如何测试$ mdDialog的 onComplete 回调?我设法测试是否调用了$ mdDialog.show但是我无法让测试回调起作用。
答案 0 :(得分:4)
$mdDialog.show()
在单元测试期间无法自行解决。要模拟该行为,您需要覆盖测试中的show方法并自行调用onComplete函数。一个简单的例子看起来像:
$mdDialog.show = function (options) {
options.onComplete();
};
// Call your function
showDialog();
然而,这忽略了$mdDialog.show()
通常返回的承诺。支持promise链的更完整的覆盖,即$mdDialog.show().then()
,需要返回一个伪承诺并手动解析它:
// Create a dummy promise we can resolve manually
var deferred = $q.defer();
$mdDialog.show = function (options) {
options.onComplete();
// Return the dummy promise
return deferred.promise;
};
// Call your function
showDialog();
// Resolve the promise, which won't actually be resolved until apply is called
deferred.resolve();
$rootScope.$apply();