javascript 如何在 Jasmine JS 中重用 beforeEach/afterEach?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17317839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:52:14  来源:igfitidea点击:

How to reuse beforeEach/afterEach in Jasmine JS?

javascripttestingjasminedry

提问by Adi Roiban

When writing tests with JasmineJS I have many tests that have similar beforeEach/afterEach code.

使用 JasmineJS 编写测试时,我有许多测试具有类似的 beforeEach/afterEach 代码。

Is there a way to implement an inheritance model using JasmineJS test suites?

有没有办法使用 JasmineJS 测试套件实现继承模型?

I can group all tests in a single describebut in this case I will end with a single HUGE JS file containing all tests.

我可以将所有测试组合成一个,describe但在这种情况下,我将以一个包含所有测试的巨大 JS 文件结束。

I would like to split the tests for each page.

我想为每个页面拆分测试。

Here is an example:

下面是一个例子:

describe('Services Page', function() {

    beforeEach(function() {
        login_as_admin()
    })

    beforeEach(function() {
        browser().navigateTo('/services')
    })

    if('Some test for services page', function() {})

    afterEach(function() {
        logout()
    })

})


describe('Administrators Page', function() {

    beforeEach(function() {
        login_as_admin()
    })

    beforeEach(function() {
        browser().navigateTo('/administrators')
    })

    if('Some test for administrators page', function() {})

    afterEach(function() {
        logout()
    })

})

采纳答案by Leo Gallucci

I think this is partially examined in this blog postand also answered herebut i'm adding an adapted answer for your example:

我认为这在这篇博文中得到了部分检查也在这里得到了回答,但我正在为您的示例添加一个改编的答案:

Reusable code:

可重用代码:

function sharedSetup(startPage) {
    beforeEach(function() {
        login_as_admin();
        browser().navigateTo(startPage);
    });

    afterEach(function() {
        logout();
    });
};

How to use it:

如何使用它:

describe('Services Page', function() {
    sharedSetup('/services');

    it('Some test for services page', function() {});
});

describe('Administrators Page', function() {
    sharedSetup('/administrators');

    it('Some test for administrators page', function() {});
});

回答by Steven

If you want to do this for all your suites, you can register a beforeEachor afterEachfunction in the topSuite:

如果您想为所有套件执行此操作,您可以在以下位置注册一个beforeEachafterEach函数topSuite

jasmine.getEnv().topSuite().beforeEach({fn: function() {
   //log in as admin
}});

If you only want to apply it on some suites, you can work with sub-suites:

如果您只想将其应用于某些套件,则可以使用子套件:

describe("as_admin", function() {
  beforeEach(function() {
    //log in as admin
  });

  describe('Services Page',function() {...});
  describe('Administrators Page',function() {...});

}

回答by Gregg

Jasmine does allow you to put beforeEachand afterEachoutside of a describecall. In this way you can have setup and teardown that is global for all of your specs. Your logout()call seems like it might be a good candidate for global teardown, and if all of your specs login as an admin, you could move that out to global scope as well.

Jasmine 确实允许您放置beforeEachafterEach退出describe呼叫。通过这种方式,您可以对所有规格进行全局设置和拆卸。您的logout()电话似乎很适合全局拆卸,如果您的所有规范都以管理员身份登录,您也可以将其移至全局范围。

For things that are used in some, but not all, specs, having a method like your login_as_admin()seems like the best way to consolidate that logic in one place.

对于在某些(但不是全部)规范中使用的东西,拥有像您login_as_admin()这样的方法似乎是将这种逻辑整合到一个地方的最佳方式。

回答by xst

Reference: (Pivotal Labs Blog:Davis W. Frank)

参考:(Pivotal Labs 博客:Davis W. Frank

He describes collecting shared functionality in a function that is called with parameters for the different individual suites. Calling this function within each suite will execute the common setup/configuration.

他描述了在一个函数中收集共享功能,该函数通过不同的单个套件的参数调用。在每个套件内调用此函数将执行通用设置/配置。

As to splitting tests across files; the file with the shared function can either be included within each page with a <script>tag if the tests are browser based, or by a require(...)near the top if the tests are node based. You can then run the tests independently but using that shared setup which is defined only once.

至于跨文件拆分测试;<script>如果测试是基于浏览器的,则具有共享功能的文件可以包含在每个页面中并带有标签,如果测试是基于require(...)节点的,则可以包含在靠近顶部的标签中。然后您可以独立运行测试,但使用仅定义一次的共享设置。