Jasmine Ajax request.respondWith not working

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

    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 am trying to call the responseWith method of the request. Although I can see that the object returned from jasmine.Ajax.requests.mostRecent () is of type FakeXMLHttpRequest, responseWidth is marked undefined. Any ideas? Thanks to

[UPDATE] I was able to narrow it down. It looks like the mock-ajax.js file is not loading. I have the karma-jasmine-ajax node module installed and added jasmine-ajax to the karma.conf.js frame array as follows:

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

      

is there anything else i need to do?

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

+3


source to share


3 answers


I faced the same problem, it looks like the latest version has no method respondWith

.

Downgrading the plugin jasmine-ajax

did the trick:



npm install karma-jasmine-ajax@0.1.1

      

Now I can see what is respondWith

working fine.

+1


source


This seems to have been a transient issue in one of the jasmine-ajax libraries. Updating to the 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

+1


source


If you are unable to change versions jasmine-ajax

due to conflicts (ex: you are using Jasmine 1.3 or something else that has a dependency on another version jasmine-ajax

), you can use: instead request.response

request.respondWith

0


source







All Articles