混淆嵌套测试套件和规范的执行顺序

confuse about the execution order of nested testing suite and specs

全部:

刚开始第二天学习Jasmine,有一个关于exe顺序的问题想弄明白:

本例来自Jasmine 2.0介绍: Jasmine 2.0 Introduction

describe("Asynchronous specs", function() {
  var value;
  beforeEach(function(done) {
    setTimeout(function() {
      value = 0;
      done();
    }, 1);
  });
  it("should support async execution of test preparation and expectations", function(done) {
    value++;
    expect(value).toBeGreaterThan(0);
    done();
  });
  describe("long asynchronous specs", function() {
    var originalTimeout;
    beforeEach(function() {
      originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
      jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
    });
    it("takes a long time", function(done) {
      setTimeout(function() {
        done();
      }, 9000);
    });
    afterEach(function() {
      jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
    });
  });
});

我尝试阅读源代码,但这只会让我更加困惑,我什至混淆了我不理解的部分....ToT

谁能简单解释一下 jasmine 在遇到 describebeforeEach 和 it 时会做什么,以及它是如何运行的?

谢谢

describe就像一个"test-scope",用来决定在哪个it上执行beforeAll,afterAll,beforeEach,afterEach,也可以简单的用来分类你的测试,因为它有点像 "chapter",有一个标题。

第一个beforeEach在第一个describe(和describe children)中的每个it之前执行。

第一个 it 可以不用 done 参数编写,因为它只包含同步操作。

然后beforeEach里面的"child"describe在每个测试用例里面执行之前,然后是里面的it,最后是afterEach,就是仅在 child describe.

中的每个 it 之后执行

总而言之,在您的示例中,函数按以下顺序执行:

beforeEach1 > it1 > beforeEach1 > beforeEach2 > it2 > afterEach

当您传递 done 参数时,jasmine "waits" 将执行以继续下一个测试用例 (it)。正如你在最后一个案例中看到的,jasmine 必须等待 9000ms 才能执行 done():默认情况下,jasmine 在 5000ms 后超时,这就是我们将其更改为 10000 的原因。

我希望已经清楚了,如果还不够,请随时询问详细信息:)