Jasmine:为什么beforeEach()在嵌套的describe中起作用,但beforeAll()却没有?

时间:2016-06-02 14:47:40

标签: angularjs unit-testing jasmine

我很难理解为什么我的代码无法工作以及为什么在嵌套的测试测试套件中使用简单的beforeAll()而不是beforeEach()时测试会失败?这是一个用于概述我的问题的小例子:

describe("myService", function() {
  // Basic services
  // Some variables 

  beforeEach(module('app'));   // Invoke the module
  beforeEach(function(){

    // Inject the services
    inject(function(_myService_) {
      myService = _myService_;
    });
  });

  /************************ Add more unit tests here ************************/

  describe("myFunction", function() {
    describe("calls function with one set of input paramenters", function() {

      //beforeAll(function() {
      beforeEach(function() { // <-- Why this works but beforeAll doesn't???
        // Call myFunction with different parameters
        result = myService.myFunction(parametersType_1);
      });

      it("should do tests on result (the return from the function)", function() {
      });
    });

  describe("calls function with other set of input paramenters", function() {

    //beforeAll(function() {
    beforeEach(function() { // <-- Why this works but beforeAll doesn't???
      // Call myFunction with different parameters
      result = myService.myFunction(parametersType_2);
    });

    it("should do tests on result (the return from the function)", function() {
    });
  });
}); 

2 个答案:

答案 0 :(得分:4)

将注入服务的部分更改为beforeAll而不是beforeEach:

beforeAll(function(){

    // Inject the services
    inject(function(_myService_) {
    myService = _myService_;
});

外部描述中的beforeEach在每个嵌套的描述部分之前都不会触发,它会在每个部分之前触发,然后才会触发。在描述中。因为内部beforeAll在外部描述中的beforeEach之前被触发,所以您在注入之前尝试使用该服务。

例如:

describe("outer describe", function() {

 beforeAll(function() {
    console.log("A");   
 });

 beforeEach(function() {
    console.log("B");   
 });

 describe("inner describe", function() {
    beforeAll(function() {
        console.log("C");
    });

    beforeEach(function() {
        console.log("D");
    });

    it("test", function() {
    })'
 });

});

将按顺序执行:A,C,B,D

答案 1 :(得分:0)

我只是想通过图像和输出来改善@Andrew的答案。

beforeAll-Each的资源:http://breazeal.com/blog/jasmineBefore.html

柱塞链接为:https://plnkr.co/plunk/wARTBcGQJitmOALs

describe('outer describe', function () {
  beforeAll(function () {
    console.log('A: outer desc->before-All');
  });

  beforeEach(function () {
    console.log('B: outer desc 1->before-Each');
  });

  afterAll(function () {
    console.log('AA: outer desc->after-All');
  });

  afterEach(function () {
    console.log('BB: outer desc 1->after-Each');
  });

  describe('inner describe 1', function () {
    beforeAll(function () {
      console.log('C: inner desc 1->before-All');
    });

    beforeEach(function () {
      console.log('D: inner desc 1->before-Each');
    });

    afterAll(function () {
      console.log('CC: inner desc 1->after-All');
    });

    afterEach(function () {
      console.log('DD: inner desc 1->after-Each');
    });

    it('test1', function () {
      console.log('inner desc 1 -> test 1');
      expect(false, 'olmadı');
    });

    it('test2', function () {
      console.log('inner desc 1 -> test 2');
      expect(false, 'olmadı');
    });
  });

  describe('inner describe2', function () {
    beforeAll(function () {
      console.log('E: inner desc 2->before-All');
    });

    beforeEach(function () {
      console.log('F: inner desc 2->before-Each');
    });

    afterAll(function () {
      console.log('EE: inner desc 2->after-All');
    });

    afterEach(function () {
      console.log('FF: inner desc 2->after-Each');
    });

    it('test1', function () {
      console.log('inner desc 2 -> test 1');
      expect(false, 'olmadı');
    });

    it('test2', function () {
      console.log('inner desc 2 -> test 2');
      expect(false, 'olmadı');
    });
  });
});

enter image description here