如何让jasmine测试加载数据并以正确的顺序执行 [英] How to get jasmine tests to load data and execute in the right order

查看:275
本文介绍了如何让jasmine测试加载数据并以正确的顺序执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jasmine(2.3.2)和Protractor(2.5.1)进行UI测试。我需要加载数据并为该数组中的每个项生成测试。我有一个函数( checkItem ),它为每个项创建一个新的 describe 函数,并检查该对象的某些属性。这是我的代码的一个非常简化的版本,还有其他测试在这些之前运行。

I'm using Jasmine (2.3.2) and Protractor (2.5.1) for my UI tests. I need to load in data and generate tests for each item from that array. I have a function (checkItem) that creates a new describe function for each item and checks certain properties on that object. This is a very simplified version of my code and there are other tests that run before these.

如果我调用 checkItem describe 语句中, itemsArr 尚未定义。所以我不确定如何加载数据并从该数据创建动态测试。

If I call checkItem in a describe statement, itemsArr isn't defined yet. So I'm not sure how to get the data loaded and create dynamic tests from that data.

var data = require('get-data'); //custom module here

describe('Test', function() {
  var itemsArr;
  beforeAll(function(done) { 
    data.get(function(err, result) {
      itemsArr = result; //load data from module
      done();
    });
  })

  //error: Cannot read property 'forEach' of undefined
  describe('check each item', function() {
    itemsArr.forEach(function(item) {
      checkItem(item);
    });
  });

  function checkItem (item) {
    var itemName = item.name;
    describe(itemName, function() {
      console.log('describe');
      //this doesn't fire when 'should check item' is called
      it('should work', function() {
        console.log('it');
        expect(false).toBeTruthy();
      });
    });
  }

});

UPDATE :当我像这样更改我的代码时,问题是同样的,所以也许还有另一种方法来加载数据而不是使用 beforeAll / beforeEach

UPDATE: When I change my code like this it's the same issue, so maybe there's another way to load data instead of using beforeAll/beforeEach

beforeAll(function() {
  itemsArr = [
    {
      name: 'apple'
    },
    {
      name: 'orange'
    },
    {
      name: 'banana'
    }
  ]
});


推荐答案

您收到的是未定义的 itemsArr 导致内部 describe 中的代码在此过程的早期和 beforeAll 完成获取数据。有代替 describe 会让它等待 beforeAll 完成。

You are getting an undefined itemsArr cause the code in the inner describe executes very early in the process and before the beforeAll finishes getting the data. Having it in place of describe would let it wait for beforeAll to finish.

它的问题现在是你不能嵌套秒 - 茉莉不会执行内部的,请参阅此相关的问题

The problem with it now is that you cannot have nested its - jasmine would not execute the inner its, see this relevant issue.

如果你不动态创建内部 s,但只是在父母中做出你的期望 it - 这会起作用。工作样本:

If you would not create inner its dynamically, but just make your expectations in the parent it - this is going to work. Working sample:

describe('Test', function() {
    var itemsArr;
    beforeAll(function(done) {
        itemsArr = [
            {
                name: 'apple'
            },
            {
                name: 'orange'
            },
            {
                name: 'banana'
            }
        ];
        done();
    });

    it('should check each item', function() {
        itemsArr.forEach(function(item) {
            expect(false).toBeTruthy();
        });
    });

});

它产生3 预期为假的虚假失败。

我仍然认为如果我们尝试使用量角器的控制流,我们可能会有一个更好的解决方案。

I still think that if we would try using Protractor's Control Flow, we might have a better solution.

这篇关于如何让jasmine测试加载数据并以正确的顺序执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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