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

jha*_*amm 4 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);
  });
});
Run Code Online (Sandbox Code Playgroud)

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

Eva*_*oli 5

您需要将规范放在一个it块中.beforeEach为每个规范运行.当描述运行时,你beforeEach还没有被执行.