2016-09-13 64 views
0

我试图达到一个全球变量后。 例如:如何使用量角器和茉莉花规格中使用全局变量

var date = 0; 

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

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

他们应该都有权访问该日期。我认为date是一个描述函数内的var,对吧?你有没有小提琴这不工作? –

+0

@ScottFanetti不,我只是想从它的函数中获得值(5)(必须设置一个值)并在其他函数中进行比较(必须进行比较) – paulotarcio

+0

那么当你使用这段代码时究竟发生了什么,你期望什么即将发生? – YakovL

回答

0

it块应该不依赖于彼此。首先,因为执行是异步的,会导致您意外的行为。第二,因为单元测试应该容易准备和独立...所以当一个失败时,你知道什么实际上失败了(即你不需要查看其他单元块)

我不确定是什么你试图用你的代码实现,但在我看来,你想要的是类似下面的代码:

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