为什么Jasmine在此异步测试中不执行it()? [英] Why is Jasmine not executing it() on this async test?

查看:81
本文介绍了为什么Jasmine在此异步测试中不执行it()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试一种原型方法,该方法返回有关我通过AJAX加载的数据集的见解.

I'm trying to test a prototypal method that returns insights about a dataset I am loading via AJAX.

$.getJSON('../data/bryce.json').done(function(data) {

    insights = new Insights(data);

    describe("People Method", function() {

       console.log('it executes this far');

        it("should return complete people data", function() {

            console.log('but not this far');

            expect(insights.people()).toBeTruthy();

        });

    });

});

当我运行此测试套件时,describe()会执行,但it()不会.一般来说,我对JavaScript测试还很陌生,所以我想我做错了什么.但我不确定这是什么.

When I run this test suite, describe() executes, but not it(). I'm pretty new to JavaScript testing in general, so I imagine I'm doing something wrong. But I'm not sure what it is.

此外,由于我正在使用的数据是巨大 JSON文件,因此实际上不可能将其包含在此文件中.甚至不可能提供样本大小的版本.数据集中的每个对象长数百行.

Also, because the data I'm working with is a huge JSON file, it's not really possible to include it in this file. Nor would it be possible to even provide a sample-size version. Each object in the dataset is hundreds of lines long.

推荐答案

茉莉花采用一种排队机制,并执行所有describeit函数来排队要执行的工作.

Jasmine works off a queuing mechanism and executes all the describe and it functions queuing up work to be executed.

在Jasmine中异步工作要求您遵循某种模式.

Doing work asyncronously in Jasmine requires you to follow a certain pattern.

茉莉1.x

describe('some suite', function(){

  it('some test', function(){

     var data;

     //Execute some async operation
     runs(function(){
         $.get('myurl').done(function(d){ data = d; });
     });

     //Wait for it to finish
     waitsFor(function(){
        return typeof data !== 'undefined';
     });

     //Assert once finished
     runs(function(){
        expect(data.foo).toBe('bar');
     });

  });

});

Jasmine 1.x使用一种特殊的轮询机制来不断轮询waitsFor方法,直到它超时或返回true,然后执行最终的runs方法.

Jasmine 1.x uses a special polling mechanism to keep polling the waitsFor method until it times out, or returns true, and then executes the final runs method.

茉莉2.x

describe('some suite', function(){

  var data;

  beforeEach(function(done){
     $.get('myurl').done(function(d){ 
        data = d;

        //Signal test to start
        done();
     });
  });

  it('some test', function(done){
     expect(data.foo).toBe('bar');

     //Signal test is finished
     done();
  });

});

Jasmine 2.x有点不同,因为它使用信号机制来指示何时开始和完成测试.您的规范可以采用可选的done方法来用于同步测试.

Jasmine 2.x is a bit different, as it uses a signaling mechanism to indicate when to start and finish the test. Your specs can take in an optional done method to use for synchronizing your tests.

如果在beforeEach中使用done方法,则只有在调用该方法后,它才会开始测试.

If you use the done method in beforeEach then it will not start your test until that method is called.

如果在it函数中使用done方法,则直到调用该方法,测试才会结束.

If you use the done method in your it function, then the test will not finish until that method has been called.

这两种方法均可用于有效管理测试中的异步行为.

Both of these can be used to effectively manage asynchronous behavior within your tests.

这篇关于为什么Jasmine在此异步测试中不执行it()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆