【问题标题】:Multiple "it" inside "describe" when writing Jasmine Test (BDD)编写 Jasmine Test (BDD) 时在“describe”中包含多个“it”
【发布时间】:2016-12-03 05:08:53
【问题描述】:

我目前正在 Jasmine 中编写一个测试,使用 Karma 作为 JS 运行器。 “描述”中是否可以有多个“它”,如下所示:

describe('PhoneCat controllers', function() {

  describe('PhoneListCtrl', function(){

    it('should create "phones" model with 3 phones', function() {
      var scope = {},
          ctrl = new PhoneListCtrl(scope);

      expect(scope.phones.length).toBe(2);

    it('should create "greetings" models with 3 greeting', funciton(){
      var scope = {},
        ctrl = new PhoneListCtrl(scope);

      expect(scope.greetings.length).toBe(3);

    });
    });
  });
});

它目前失败了,但是你如何编写一个没有冗余的测试(在这种情况下,必须描述同一个控制器,两次)?

【问题讨论】:

    标签: angularjs jasmine karma-runner


    【解决方案1】:

    使用beforeEach 函数创建通用设置。它可以添加到任何级别。

    describe('PhoneCat controllers', function() {
      describe('PhoneListCtrl', function(){
        beforeEach(function() {
          this.scope = {};
          this.ctrl = new PhoneListCtrl(scope);
        });
    
        it('should create "phones" model with 3 phones', function() {
          expect(this.scope.phones.length).toBe(2);
        });
    
        it('should create "greetings" models with 3 greeting', funciton(){
          expect(this.scope.greetings.length).toBe(3);
        });
      });
    });
    

    【讨论】:

      【解决方案2】:

      我知道这个问题很古老,但 OP 说“这个测试失败了”。

      不管是否需要 ForEach(),可能是因为:

       expect(scope.phones.length).toBe(2);
      

      应该是:

       expect(scope.phones.length).toBe(**3**);
      

      【讨论】:

      • 测试失败与 OP 旨在解决的问题完全无关。 Mocha 的用户故意使测试失败。 OP 显然不是对测试失败这一事实感到困惑,而是对如何避免跨测试重复代码感到困惑。
      • @Louis - +1 教我一个新词。 :-) Tangential - 形容词 1. 属于或属于切线的性质;存在或沿切线方向移动。 2.只是触摸;微连接:切线信息。 3. 发散的或离题的,从一个正在考虑的主题:切线的评论。 4. 倾向于离题或回答问题。
      猜你喜欢
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多