【问题标题】:Teaspoon error while testing simple script测试简单脚本时出现茶匙错误
【发布时间】:2015-12-11 00:14:35
【问题描述】:

我正在测试以下代码逻辑:

handleOnMediaPlaying: function(event){
  // body...
  if(isAd){
    if(event.data.percentComplete >= 25 && !firstQuartileFlag){
      firstQuartileFlag = true;
    }
    if(event.data.percentComplete >= 50 && !midpointflag){
      midpointflag = true;
    }
    if(event.data.percentComplete >= 75 && !thirdQuartileFlag){
      thirdQuartileFlag = true;
    }
  }
},

函数“handleOnMediaPlaying”位于对象 pdkHandler 中。此外,在 pdhHandler 中还有另一个函数 handleOnMediaStart,我在其中定义变量 isAd(如果媒体是广告)、firstQuartileFlag(设置为 false)、midpointflag(设置为 false)和 thirdQuartileFlag(设置为 false)。 我已经为相同的内容编写了以下规范,但它失败并出现错误说“预计错误为真”。以下是规格....

describe("handleOnMediaPlaying", function(){

beforeEach(function(){
  isAd = true;
  firstQuartileFlag = false;
  midpointflag = false;
  thirdQuartileFlag = false; 
  spyOn(pdkHandler, 'handleOnMediaPlaying');
});

it('sets firstQuartileFlag to true on percentComplete = 26 ', function(){
  var eventAd = {
    data: {
      percentComplete: 26
    }
  };
  pdkHandler.handleOnMediaPlaying(eventAd);
  expect(firstQuartileFlag).toBe(true);
});

it('sets midpointflag to true ', function(){
  var eventAd = {
    data: {
      percentComplete: 50
    }
  };
  pdkHandler.handleOnMediaPlaying(eventAd);
  expect(midpointflag).toBe(true);
});


it('sets thirdQuartileFlag to true ', function(){
  var eventAd = {
    data: {
      percentComplete: 76
    }
  };
  pdkHandler.handleOnMediaPlaying(eventAd);
  expect(thirdQuartileFlag).toBe(true);
});

});

我不知道为什么当一切都直截了当时它会失败。请帮忙。

【问题讨论】:

    标签: jasmine teaspoon


    【解决方案1】:

    我发现了问题,spyOn(pdkHandler, 'handleOnMediaPlaying');方法正在执行函数 handleOnMediaPlaying,因此该值未设置为预期的 true。 希望这对像我这样的新手有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-08-29
      • 2021-01-25
      • 2013-06-22
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      相关资源
      最近更新 更多