ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript-在执行测试之前从模块加载数据

2019-11-19 09:37:10  阅读:265  来源: 互联网

标签:node-js jasmine asynchronous protractor javascript


(我最近问this question并接受了一个答案,但这仍然不是我所需要的.)我确实需要根据从模块加载的数据创建动态测试.数组中的每个项目都有其自己的带有某些量角器动作的describe语句.我以前的帖子给出了一个答案,该答案说要使用it语句,但是我不能这样做,因为发生了太多事情.

我的主要问题是数据没有及时加载到描述中.我还有另一个建议使用VCR.js或类似的东西,但我认为这些不会起作用,因为我正在使用模块.有没有一种方法可以将数据保存到单独的文件中并加载到其中?那是个好方法吗?

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');
      it('should work', function() {
        console.log('it');
        expect(true).toBeTruthy();
      });
    });
  }

});

更新:

我使用了尤金的答案,并提出了这个答案.我无法测试每个学生的学习意愿,因为it语句不会触发.这个问题甚至可以解决吗?

describe('check each item', function () {
   it('should load data', function (done) {
      browser.wait(itemsPromise, 5000);
     itemsPromise.then(function(itemsArr) {
       expect(itemsArr).toBeTruthy();
       studyArr = itemsArr.filter(function (item) {
         return item.enabled && _.contains(item.tags, 'study');
       });
       studyCount = studyArr.length;
       expect(studies.count()).toEqual(studyCount);
       checkItems(studyArr);
       done();
     });
   });
   function checkItems (itemsArr) {
     itemsArr.forEach(function (item) {
       describe(item.id, function () {
        console.log('checkItems', item.id);
        // doesn't work
         it('should work', function (done) {
           expect(false).toBeTruthy();
           done();
         });
       });
     });
   }
 });

解决方法:

您正在尝试执行Jasmine不允许的操作:在测试套件启动后生成测试.参见this comment,有关茉莉花的问题:

Jasmine doesn’t support adding specs once the suite has started running. Usually, when I’ve needed to do this, I’ve been able to know the list of options ahead of time and just loop through them to make the it calls. […]

(“添加规格” ===“添加测试”)

关键是您可以动态生成测试,但只能在测试套件开始执行测试之前.一个必然的结果是测试生成不能是异步的.

您的第二次尝试不起作用,因为它试图将测试添加到已经运行的套件中.

您的第一次尝试更接近您的需求,但它也不起作用,因为describe会立即调用其回调,因此在您的describe尝试生成测试时,beforeAll尚未运行.

解决方案

一切归结为在测试套件开始执行测试之前计算itemsArr的值.

您可以创建一个.getSync方法,该方法将同步返回结果.您的代码将如下所示:

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

describe('Test', function() {
  describe('check each item', function() {
    itemsArr.forEach(function(item) {
      checkItem(item);
    });
  });

[...]

如果无法编写.getSync函数,则可以让外部进程负责生成JSON输出,然后将其反序列化为itemsArr.您将使用child_process的… Sync功能之一执行此外部过程.

这是第二个选项如何工作的示例.我使用以下代码创建了一个get-data.js文件,该文件使用setTimeout模拟异步操作:

var Promise = require("bluebird"); // Bluebird is a promise library.

var get = exports.get = function () {
    return new Promise(function (resolve, reject) {
        var itemsArr = [
            {
                name: "one",
                param: "2"
            },
            {
                name: "two",
                param: "2"
            }
        ];
        setTimeout(function () {
            resolve(itemsArr);
        }, 1000);
    });
};

// This is what we run when were are running this module as a "script" instead
// of a "module".
function main() {
    get().then(function (itemsArr) {
        console.log(JSON.stringify(itemsArr));
    });
};

// Check whether we are a script or a module...
if (require.main === module) {
    main();
}

然后,在规格文件中:

var child_process = require('child_process');

var itemsArr = JSON.parse(child_process.execFileSync(
    "/usr/bin/node", ["get-data.js"]));

describe('Test', function() {
    itemsArr.forEach(function(item) {
        checkItem(item);
    });

    function checkItem (item) {
        var itemName = item.name;
        describe(itemName, function() {
            console.log('describe');
            it('should work', function() {
                console.log('it');
                expect(true).toBeTruthy();
            });
        });
    }
});

我已经使用jasmine-node测试了上面的代码.以及以下文件结构:

.
├── data.js
├── get-data.js
└── test
    └── foo.spec.js

./node_modules中包含bluebird和茉莉花节点.这是我得到的:

$./node_modules/.bin/jasmine-node --verbose test 
describe
describe
it
it

Test - 5 ms

    one - 4 ms
        should work - 4 ms

    two - 1 ms
        should work - 1 ms

Finished in 0.007 seconds
2 tests, 2 assertions, 0 failures, 0 skipped

标签:node-js,jasmine,asynchronous,protractor,javascript
来源: https://codeday.me/bug/20191119/2035220.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有