关于javascript:我如何在Jasmine中使用beforeAll函数? (不是coffeeScript)

How can I have a beforeAll function in Jasmine ? (Not coffeeScript)

我需要知道是否可以包含或使用beforeAll函数或类似的方法,因此我可以登录到我的应用程序,然后开始测试。

现在,我将登录操作放在第一个测试用例(it)中。这不是一个好习惯。

如果还有其他更好的方法来存储我的登录代码,那么可以使用beforeAll函数,告诉我有关信息。

我正在使用与其他框架(例如coffee-script或其他框架)无关的纯Jasmine。

谢谢


现在,这变得容易得多。从Jasmine 2.1(2014年11月14日发布)开始,框架中内置了beforeAll函数。

以下是发行说明,其中包含2.1中添加的所有内容。这是解释beforeAllafterAll

的文档


您可以根据需要嵌套任意多个describe函数。所以你可以做类似...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
describe("General Test", function () {

    function login(){
        //This code will run once at he beginning of your script
    };

    login();

    beforeEach(function () {
        //anything in here will apply to everything in each nested describe
    });

    describe("Specific Test", function () {
        //Applied here
    });

    describe("Another Specific Test", function () {
        //And here
    });


});


您可以添加此程序包,该程序包向Jasmine添加beforeAll()和afterAll()。

https://github.com/nonplus/jasmine-beforeAll


请使用下面的代码并在beforeAll语句中配置您的设置。

1
2
3
4
5
6
7
8
describe("Top", function() {
     beforeAll(function() {
            console.log("Example 1 Setup");
     });
     it('xyz',function(){
        console.log('Hi!')
     });
});