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

蹲街弑〆低调 提交于 2019-12-03 10:48:09

问题


I need to know if there is a way to include or use a beforeAll function, or something similar, so I can login to my application and then start testing.

Right now I'm putting my login operations in the first test case ( it ). Which is not a good practice.

If there is a better way to store my login code other then using a beforeAll function please tell me about it.

I'm using pure Jasmine not related to any other framework like coffee-script or others.

Thank you


回答1:


This is now much easier. As of Jasmine 2.1 (released 14 Nov 2014), there is a beforeAll function built into the framework.

Here are the release notes with everything that was added in 2.1. And here is the documentation explaining beforeAll and afterAll




回答2:


You can nest as many describe functions as you want. So you can do something like...

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


});



回答3:


You can add this package that adds a beforeAll() and afterAll() to Jasmine.

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



来源:https://stackoverflow.com/questions/24208490/how-can-i-have-a-beforeall-function-in-jasmine-not-coffeescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!