本文介绍了嵌套在量角器/茉莉花中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在量角器/茉莉花中创建嵌套吗.

Can I create a nested it in Protractor/Jasmine.

it("outer it", function () {
    it("inner it", function () {
        expect(1).toBe(1);
    });
});

我试图在一个循环内执行它的测试用例,并且在每个迭代中我都想运行一个测试,例如:

I am trying to execute it test cases inside a loop, and in every iteration I want to run a test, for example:

it("outer it", function () {
    for(var i=0;i<10;i++){
        it("inner it", function () {
            expect(1).toBe(1);
        });
    }
});

我要这样做的原因是,我想初始化一个数组,然后以一种动态的方式循环遍历所有元素并运行多个"it",例如:

The reason I want to do it is that I want to initialize an array and then in a dynamically way to loop trough all the element and run a number of "it", for example:

describe ("[Components]", function() {
   var grid = new Grid();

   it("Initialize the grid for the history window", function () {
       grid.init();
   });

   for(var i=0;i<grid.length;i++){
       it("test 1", function () {
           expect(1).toBe(1);
       });
   }

});

当for循环执行时,grid.length等于0,我希望for循环在初始化"it"之后执行.

The grid.length is equal to 0 when the for loop execute, I want the for loop execute after the initialize "it".

推荐答案

回答您的问题,,您不能将的一个嵌套在另一个内部.尽管Jasmine框架不会引发任何错误,但嵌套的 中的代码不会执行.另外,我看不到嵌套 it 的任何用途,因为它们是自行运行以完成特定测试步骤的规范或功能.它还概述了当前正在执行的功能.如果您试图在循环中运行某些内容,则可以创建另一个函数,然后在for循环中调用它,就像这样-

Answering to your question, no you cannot nest it's one inside other. Though Jasmine framework doesn't throw any error, the code inside a nested it doesn't execute. Also, I don't see any use of nesting it's as they are specs or functions that run on their own to complete a particular test step. It also gives an overview of the function that is being executed currently. If you are trying to run something in a loop, you can create another function and then call it inside the for loop, something like this -

it("outer it", function () {
    var newFunction = function(){
        expect(1).toBe(1);
    };
    for(var i=0;i<10;i++){
        newFunction();
    };
});

希望这会有所帮助.关于它的更多信息,可以在这里找到- Jasmine Framework-是

Hope this helps. More on it's can be found here - Jasmine Framework - it's

这篇关于嵌套在量角器/茉莉花中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 16:44