0

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

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)。我已经为相同的内容编写了以下规范,但它失败并出现错误说“预计错误为真”。下面是规格......

描述(“handleOnMediaPlaying”,函数(){

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);
});

});

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

4

1 回答 1

0

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

于 2015-12-11T19:05:11.940 回答