简体   繁体   English

如何抽象和重用茉莉花中的describe()块?

[英]how to abstract and re-use a describe() block in jasmine?

I work on an app that handles CRUD on a certain type of documents. 我开发的应用程序可以处理某些类型的文档的CRUD。 Therefore we want often to test the conformity of that document against a set of values. 因此,我们经常需要针对一组值来测试该文档的符合性。 I have a describe() block with it() inside that are testing each field of that document. 我有一个describe()块,里面带有it(),用于测试该文档的每个字段。

I would like to make that re-usable, but simply wrapping a function around that describe block leads to timing issues, and the following test are running too early, before the tests in that wrapping function. 我想使它可重用,但是只是在描述块周围包装一个函数会导致计时问题,并且在该包装函数中进行测试之前,以下测试运行得太早了。

Is there a pattern to do this ? 有这样做的模式吗? Should it be a promise ? 这应该是一个承诺吗? Should I make of it a matcher ? 我应该把它做成匹配器吗? docToBeEqualTo() ? docToBeEqualTo()? I haven't seen it() used within matchers, it does not seems to be the right place for this 我还没有看到在匹配器中使用过it(),这似乎并不是正确的选择

I guess I am missing a common pattern here. 我想我在这里错过了一个常见的模式。 can you help ? 你能帮我吗 ?

thanks a lot 非常感谢

edit: requested code exemple : 编辑:要求的代码示例:

function compareDocs(targetData, timeout){
        describe('the doc should contain : ', function(){
            it('The amount field should be : "' + targetData.amount + '". ', function(){
                expect(element(by.css(selector)).getAttribute('value')).toEqual(targetData.amount);
            }, timeout);


            it('The foo bar number  should be : "' + targetData.fooBar + '". ', function(){
                expect(element(by.css(selector)).getAttribute('value')).toEqual(targetData.fooBarNumber);

            }, timeout);

            return browser.waitForAngular();
        });
});

used like this: 像这样使用:

describe('first describe', function(){
            it('compare docs', function(){
               compareDocs(currentData,targetData);
            }, timeout);
});
describe('second describe where the doc is deleted', function(){
        it('should remove payment from list', function(){
              deleteDoc();//this runs too early, and delete the doc before compareDocs() has finished
              expect(element(by.css(selector)).isPresent).toBe(false);              
            }, timeout);
        });

TL;DR : how to prevent deleteDoc() to run before compareDoc() has finished ? TL; DR:如何防止deleteDoc()compareDoc()完成之前运行?

Jasmine always checks for blocks of describe and nested describe functions. 茉莉花总是检查describe和嵌套describe功能块。 However, when you define a suite( describe ) or spec( it ) inside a function that executes only on being called, protractor doesn't recognise the suite or spec in the function specified. 但是,当您在仅在被调用时执行的函数内定义suite( describe )或spec( it )时,量角器无法在指定的函数中识别出Suite或spec。 You can either use loops or self executing functions to execute suites and specs (This wouldn't solve your problem i suppose). 您可以使用循环或自执行函数来执行套件和规范(我想这不会解决您的问题)。 Only possible solution is to call a function that has block of code without any suites or specs in it. 唯一可行的解​​决方案是调用一个具有代码块但没有任何套件或规格的函数。 Here's an example - 这是一个例子-

function compareDocs(targetData, timeout){
    console.log('The amount field should be : "' + targetData.amount + '". ');
    expect(element(by.css(selector)).getAttribute('value')).toEqual(targetData.amount);
    console.log('The foo bar number  should be : "' + targetData.fooBar + '". ');       
    expect(element(by.css(selector)).getAttribute('value')).toEqual(targetData.fooBarNumber);
    return browser.waitForAngular();
};

NOTE: A describe suite nested inside an it spec won't execute. 注意:嵌套在it规范内的describe套件将不会执行。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM