如何断言我的Jasmine测试中调用了一个方法

时间:2014-07-16 16:41:28

标签: javascript angularjs jasmine angular-strap

我在指令

中有这段代码
var modalPromise = $modal({template: '/Templates/Alerts/Upsell.html', persist: true, show: false, backdrop: 'static', scope: scope});
                                $q.when(modalPromise).then(function(modalEl) {
                                    modalEl.modal('show');
                                });

在我的Jasmine测试中,我想断言调用show方法,所以我有这个

...

.service('modal', function () { 
              var $modal = jasmine.createSpyObj('$modal', ['show']);
              return $modal;
          })

...

expect($modal('show')).toHaveBeenCalled();

但是这给了我错误Cannot read property 'protocol' of undefined.

我想我在这里做错了什么。我如何断言正在调用此行?

1 个答案:

答案 0 :(得分:1)

一个笨蛋或小提琴会帮助到这里,但这是我的假设。在监视$modal show后,您还必须致电andCallThrough(),以便将所有后续调用委托给$modal的实际实施。像:

.service('modal', function () { 
    var modal;
    spyOn(modal, 'show').andCallThrough();

    return modal;
})