如何在 Jasmine JS 中重用 beforeEach/afterEach? [英] How to reuse beforeEach/afterEach in Jasmine JS?

查看:23
本文介绍了如何在 Jasmine JS 中重用 beforeEach/afterEach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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

I would like to split the tests for each page.

这是一个例子:

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()
    })

})

推荐答案

我认为这是部分检查 在这篇博文中 并且在这里回答 但我正在添加一个改编的回答你的例子:

I think this is partially examined in this blog post and also answered here but i'm adding an adapted answer for your example:

可重用代码:

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

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

使用方法:

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() {});
});

这篇关于如何在 Jasmine JS 中重用 beforeEach/afterEach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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