Unable to spy function inside ajax success callback and forEach.

253 views
Skip to first unread message

tiag...@sportlobster.com

unread,
Jan 7, 2016, 10:15:51 AM1/7/16
to Jasmine

thanks in advance, I am trying to test a function that is called on ajax success callback and then inside a forEach. (trackTaxonomyEvent())

But I am always getting

Error: Expected spy trackTaxonomyEvent to have been called

If I move the function out of the forEach it works.

code.js

saveFanzones: function(closeOnSave) {
    this.selectedTaxonomyCollection.create({
        taxonomy_ids: newTaxaonomiesId.join(',')
    }, {
        success: function() {
            _.forEach(this.selectedTaxonomyCollection.where({
                user_selected: true
            }), function(fanzoneModel) {

                var params = {
                    id: fanzoneModel.get('id')
                };

                this.trackTaxonomyEvent('fanzone:join',params);
            }.bind(this));
        }.bind(this)
    });
});

code.spec.js

it('Ajax call correct URL', function() {
    spyOn($, 'ajax').and.callFake(function(params) {
        params.success('1');
    });
    spyOn(view, 'trackTaxonomyEvent').and.callThrough();

    view.saveFanzones();
    expect(view.trackTaxonomyEvent).toHaveBeenCalled();
});

Gregg Van Hove

unread,
Feb 16, 2016, 7:22:29 PM2/16/16
to jasmi...@googlegroups.com
Typically, when I've seen this sort of behavior, it's because the code under test saves off a reference to the actual function being spied on which isn't something that jasmine can update. Everything in your example looks like it should work as you expect. Do you have a more complete example (anonymized however makes sense to you) that you can share to help debug the issue?

Thanks for using Jasmine!

-Gregg

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages