在jasmine指定的超时时间内未调用异步回调.DEFAULT_TIMEOUT_INTERVAL



我使用主干提取调用来提取数据。当我尝试使用jasmine测试它时,我得到了一个错误:Timeout-异步回调没有在jasmine指定的超时内调用。DEFAULT_TIMEOUT_INTERVAL

当我控制台valueReturned时,它为我提供了我需要的实际值。响应中没有错误。但我在页面上看到的唯一一件事是我上面指定的超时错误。

你能告诉我我做错了什么吗?

describe("fetch call", function() {
var valueReturned;
beforeEach(function(done) {
window.jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
setTimeout(function () {
todoCollectionCursor.fetch({
success : function(collection, response, options) {
valueReturned = options.xhr.responseText; 
return valueReturned;
}
});
},500);
});
it("should return string", function(done) {
console.log(valueReturned);
expect(typeof valueReturned).toEqual('string');
});
});

试试这个:

describe("fetch call", function() {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
var valueReturned;
beforeEach(function(done) {
setTimeout(function () {
todoCollectionCursor.fetch({
success : function(collection, response, options) {
valueReturned = options.xhr.responseText; 
return valueReturned;
}
});
},500);
});
it("should return string", function(done) {
console.log(valueReturned);
expect(typeof valueReturned).toEqual('string');
});
});

此外,我不确定它是否是有意的,但我希望你在之前,而不是之前。所有在这个例子中,它都会更适合,因为没有设置或任何初始化

如果你的代码不起作用,试试这个设置,它对我有效,并相应地更新你的代码

describe("Testing app: ", function () {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;
beforeAll(function (done) {
setTimeout(done, 10000);
});

it("Verify: true is true with 10 sec delay ", function (done) {
setTimeout(true.toBe(true), 10000);
done();
});
});

我找到了答案。由于这是一个Ajax调用,我应该模拟这个请求。这是我在jasmine Ajax下发现的-https://github.com/jasmine/jasmine-ajax

it("Response check response when you need it", function() {
var doneFn = jasmine.createSpy("success");
$.ajax({
url: '/fetch',
method: 'GET',
success : function(data)
{
doneFn(data);
}
})
jasmine.Ajax.requests.mostRecent().respondWith({
"status": 200,
"contentType": 'text/plain',
"responseText": '[{title:"Aravinth"}]'
});
expect(jasmine.Ajax.requests.mostRecent().responseText).toEqual('[{title:"Aravinth"}]');
});

最新更新