Julio Garcia
Julio Garcia

Reputation: 1954

Jasmine Ajax request.respondWith is not working

I am using Jasmine 2.0.4 with jasmine-ajax 2.99.0 to try to test a module that calls a web service. The code is the following:

    define(['models/data-service', 'models/admin', 'models/contest', 'models/participant', 'ContestResponse'],
    function(dataService, admin, Contest, Participant, ContestResponse){
    "use strict";

        describe("Data Service Tests", function(){
            var onSuccess, onFailure, request;

            describe("on new contests loaded", function(){
                beforeEach(function(){
                    jasmine.Ajax.install();
                });

                it("calls onSuccess with an array of Contests", function(){
                    onSuccess = jasmine.createSpy('onSuccess');
                    onFailure = jasmine.createSpy('onFailure');
                    dataService.getContests()
                        .done(onSuccess)
                        .fail(onFailure);

                    request = jasmine.Ajax.requests.mostRecent();
                    expect(request.url).toBe('/api/contest');
                    expect(request.method).toBe('GET');
                    request.respondWith(ContestResponse.getResponse().contest.success);
                    expect(onSuccess).toHaveBeenCalled();
                    var successArgs = onSuccess.calls.mostRecent().args[0];
                    expect(successArgs.length).toEqual(4);
                });
            });
        });
});

Everything works until it reaches the line where I try to call the respondWith method of the request. Even though I can see that the object returned from the jasmine.Ajax.requests.mostRecent() is of type FakeXMLHttpRequest, respondWidth is marked as undefined. Any Ideas? Thanks

[UPDATE] I have been able to narrow it down. It looks like the mock-ajax.js file is not being loaded. I have the karma-jasmine-ajax node module installed and have added jasmine-ajax to the frameworks array of the karma.conf.js like this:

frameworks: ['jasmine-ajax','jasmine', 'requirejs'],

is there anything else I need to do?

[RANT] no wonder why so few developers are running unit test with javascript [/RANT]

Upvotes: 3

Views: 2824

Answers (3)

Kyle Chadha
Kyle Chadha

Reputation: 4151

If you can't change jasmine-ajax versions due to conflicts (ie: you use Jasmine 1.3 or something else that has a dependency on a different version of jasmine-ajax), you can use: request.response instead of request.respondWith

Upvotes: 0

Artif3x
Artif3x

Reputation: 4661

This appears to have been a transitional problem in one of the jasmine-ajax libraries. Upgrading to latest version (3.1.0 at the time of this writing) will fix your problem.

npm install jasmine-ajax
bower install jasmine-ajax

or jasmine-ajax on github

Upvotes: 1

Ant's
Ant's

Reputation: 13801

I faced the same issue, it looks like the latest version doesn't have respondWith method.

Downgrading the jasmine-ajax plugin made the trick:

npm install [email protected]

Now I can see respondWith is working fine.

Upvotes: 1

Related Questions