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

时间:2017-08-30 19:12:34

标签: backbone.js jasmine

我使用骨干提取调用获取数据。当我尝试使用jasmine测试它时,我收到错误错误:超时 - 在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');
});
});

2 个答案:

答案 0 :(得分:0)

试试这个:

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();
    });
});

答案 1 :(得分:0)

我找到了答案。因为它是一个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"}]');
   });
相关问题