Sachin Kainth
Sachin Kainth

Reputation: 46760

How to assert that a method is called in my Jasmine test

I have this snippet of code in a directive

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');
                                });

In my Jasmine test I want to assert that the show method is called, so I have this

...

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

...

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

But this gives me the error Cannot read property 'protocol' of undefined.

I think I am doing something wrong here. How do I assert that this line is being called?

Upvotes: 0

Views: 137

Answers (1)

dmahapatro
dmahapatro

Reputation: 50265

A plunker or fiddle would have helped here but here is my assumption. After spying the $modal for show you also have to call andCallThrough() to allow all subsequent calls to be delegated to actual implementation of $modal. Like:

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

    return modal;
})

Upvotes: 1

Related Questions