【问题标题】:Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时时间内未调用异步回调
【发布时间】:2017-08-30 19:12:33
【问题描述】:

我正在使用主干获取调用来获取数据。当我尝试使用 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');
});
});

【问题讨论】:

    标签: backbone.js jasmine


    【解决方案1】:

    试试这个:

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

    另外,我不确定它是否是有意的,但不是 beforeEach,我会在此示例中使用 beforeAll,因为没有设置或任何初始化,它会更适合

    如果您的代码不起作用,请尝试此设置,它适用于我,并相应地更新您的代码

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

    【讨论】:

    • 谢谢。我尝试使用 beforeAll 和您提供的代码。但它没有用。
    • 您是否尝试将 jasmine.DEFAULT_TIMEOUT_INTERVAL 从 beforeEach 中移出?
    • 是的,我正在检查。
    • 谢谢kryp 我找到了上述问题的答案。
    【解决方案2】:

    我找到了答案。由于它是一个 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"}]');
       });
    

    【讨论】:

      猜你喜欢
      • 2016-04-21
      • 2014-05-01
      • 2019-11-04
      • 1970-01-01
      • 2018-07-30
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多