Jasmine Ajax request.respondWith不起作用 [英] Jasmine Ajax request.respondWith is not working
问题描述
我正在使用Jasmine 2.0.4和jasmine-ajax 2.99.0来尝试测试一个调用Web服务的模块。
代码如下:
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);
});
});
});
});
一切正常,直到它到达我尝试调用请求的respondWith方法的行。尽管我可以看到从jasmine.Ajax.requests.mostRecent()返回的对象是FakeXMLHttpRequest类型,但respondWidth被标记为undefined。有任何想法吗?
谢谢
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
[更新]
我能够缩小范围。看起来像是没有加载mock-ajax.js文件。我安装了karma-jasmine-ajax节点模块,并将jasmine-ajax添加到karma.conf.js的frameworks数组中,如下所示:
[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]难怪为什么这么少的开发人员使用javascript运行单元测试[/ RANT]
[RANT] no wonder why so few developers are running unit test with javascript [/RANT]
推荐答案
我遇到了同样的问题,看起来最新版本没有 respondWith
方法。
I faced the same issue, it looks like the latest version doesn't have respondWith
method.
降级 jasmine-ajax
插件诀窍:
npm install karma-jasmine-ajax@0.1.1
现在我可以看到 respondWith
工作正常。
Now I can see respondWith
is working fine.
这篇关于Jasmine Ajax request.respondWith不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!