简体   繁体   中英

How to use global variable in specs using protractor and Jasmine

i'm trying to reach a global variable after some its. For example:

var date = 0;

it('must set a value', function(){
    date = 5;
});

it('must compare', function(){
     expect(date).toBe(5);
});

The it blocks shouldn't depend on each other. First because the execution is asynchronous what will lead you to unexpected behaviors. And second because the units tests should be easy to ready and independent... so when one fails, you know what actually failed (ie you don't need to look into other unit blocks)

I'm not sure what you're trying to achieve with your code but it seems to me that what you want is something like the following code:

describe('MyTestSpec', function () {

    var date = 0;

    beforeEach(function () {
        //Using beforeEach will actually assume that date will be set to 5
        //before the execution of your it-block.
        date = 5;
    });

    it('check if date is 5', function () {
        expect(date).toBe(5);     
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM