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