【问题标题】:Jasmine: why beforeEach() works in nested describe but beforeAll() doesn't?Jasmine:为什么 beforeEach() 在嵌套描述中有效,但 beforeAll() 无效?
【发布时间】:2016-06-02 14:47:39
【问题描述】:

当我在嵌套描述测试套件中使用简单的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() {
    });
  });
}); 

【问题讨论】:

    标签: angularjs unit-testing jasmine


    【解决方案1】:

    将要注入服务的部分更改为 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

    【讨论】:

    • 从严格的 Jasmine 角度来看,这是正确的。但是,当我尝试这样做时,在 BeforeAll() 块中设置 Angular 模块和服务不起作用。通过深入挖掘,我进入了这篇文章link。根据这篇文章,模块在测试之间会被重置,因此只设置一次是不应该的。
    【解决方案2】:

    我只是想通过图像和输出改进@Andrew 的回答。

    beforeAll-Each 的资源:@​​987654321@

    Plunker 链接是: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ı');
        });
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 2016-10-21
      • 2022-01-12
      • 2020-05-14
      • 2020-10-07
      相关资源
      最近更新 更多