为什么我的变量在我的Jasmine测试中未定义 - Jasmine.js

时间:2014-02-04 00:08:33

标签: javascript jasmine

我已经开始为我的js文件编写测试,但我得到的是undefined,我不指望它。这是我的代码:

describe('show jasmine testing', function() {
  var x;
  beforeEach(function() {
    x = 3;
  });

  describe('booleans', function() {
    it('should return true', function() {
      expect(true).toBe(true);
    });
  });

  describe('ints', function() {
    console.log('This is x: ' + x);
    expect(x).toBe(3);
  });
});

在我的ints测试中,我的x变量为undefined,因此测试始终失败。根据我的理解,它应该是3,因为beforeEach块在每个describe块之前运行。我错过了什么?

1 个答案:

答案 0 :(得分:5)

您需要将规范放在it块中。每个规范都会运行beforeEach。到描述运行时,您的beforeEach尚未执行。