如何在Jasmine中使用beforeAll函数? (不是coffeeScript)

时间:2014-06-13 15:04:37

标签: javascript unit-testing testing jasmine

我需要知道是否有办法包含或使用beforeAll函数或类似的东西,所以我可以登录我的应用程序然后开始测试。

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

如果有更好的方法来存储我的登录代码,那么使用beforeAll函数请告诉我。

我使用纯粹的Jasmine与任何其他框架(如咖啡脚本或其他框架)无关。

谢谢

4 个答案:

答案 0 :(得分:33)

现在这更容易了。截至Jasmine 2.1(2014年11月14日发布),框架内置了beforeAll函数。

Here are the release notes包含2.1中添加的所有内容。并here is the documentation解释beforeAllafterAll

答案 1 :(得分:3)

您可以根据需要嵌套尽可能多的describe个函数。所以你可以做点像......

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


});

答案 2 :(得分:1)

您可以添加这个将beforeAll()和afterAll()添加到Jasmine的包。

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

答案 3 :(得分:0)

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

describe("Top", function() {
     beforeAll(function() { 
            console.log("Example 1 Setup"); 
     });
     it('xyz',function(){
        console.log('Hi!')
     });
});
相关问题