$ ionicPopup.show按钮onTap功能未在Angular Jasmine单元测试中获得覆盖 [英] $ionicPopup.show buttons onTap function not getting coverage in Angular Jasmine Unit Test

查看:189
本文介绍了$ ionicPopup.show按钮onTap功能未在Angular Jasmine单元测试中获得覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在单元测试规范文件中调用outOfMvp()函数时,除onTap函数外,该函数中的所有内容都得到覆盖. 我想知道如何在我的单元测试中覆盖按钮的onTap函数,该函数在$ ionicPopup.show()方法中传递的JSON对象中?

When I call the outOfMvp() function in my unit test spec file, everything inside the function is getting coverage except for the onTap function. I was wondering how to get coverage in my unit test for the button's onTap function which is in a JSON object being passed in $ionicPopup.show() method?

这是js文件中的函数:

Here is the function in the js file:

function outOfMvp(data) {
  environmentConfig.isScanDataReadValid = false;
  popup = $ionicPopup.show({
    title: kioskConstants.POPUP_WORDING.OUT_OF_MVP_RETURN.title,
    template: data.returnRejectReasons[0].description
    + '.<br><br>' + kioskConstants.POPUP_WORDING.OUT_OF_MVP_RETURN.template
    + ' <span style=\'color:#007dc6\'>' + data.orderNo + '</span>',
    cssClass: 'popup-container-small',
    buttons: [
      {
        text: "OK",
        type: "bottom-button green narrow",
        onTap: function () {
          if ($state.current.name != "order-history") {
            environmentConfig.isScanDataReadValid = true;
            $rootScope.$broadcast("CancelConnectQR", data.orderNo);
            OrderInfo.init();
            $state.go(kioskConstants.ROUTE_STATE_LANDING_PAGE);
          }
        }
      }
    ]
  });
}

规格文件中outOfMvp函数的单元测试:

unit test for outOfMvp function in spec file:

describe('If call customPopupWidget.outOfMvp(data) function',function () {

it('it should call $ionicPopup.show().', function () {
  customPopupWidget.outOfMvp(data);
  expect(ionicPopup.show).toHaveBeenCalled();
});

outOfMvp函数的

coverage html文件:

coverage html file for the outOfMvp function:

推荐答案

您需要进行一些修改,以确保代码在单元测试的帮助下能否正常工作.

You need to modify your code a little bit to ensure whether code works or not with help of unit testing.

尝试一下:

function outOfMvp(data) {
  environmentConfig.isScanDataReadValid = false;
  popup = $ionicPopup.show({
    title: kioskConstants.POPUP_WORDING.OUT_OF_MVP_RETURN.title,
    template: data.returnRejectReasons[0].description
    + '.<br><br>' + kioskConstants.POPUP_WORDING.OUT_OF_MVP_RETURN.template
    + ' <span style=\'color:#007dc6\'>' + data.orderNo + '</span>',
    cssClass: 'popup-container-small',
    buttons: [
      {
        text: "OK",
        type: "bottom-button green narrow",
        onTap: function () {
          return true;
        }
      }
    ]
  });
  popup.then(function(response){
     if(response){ //when response is true
       if ($state.current.name != "order-history") {
         environmentConfig.isScanDataReadValid = true;
         $rootScope.$broadcast("CancelConnectQR", data.orderNo);
         OrderInfo.init();
         $state.go(kioskConstants.ROUTE_STATE_LANDING_PAGE);
       }
     }
  })
}

您可以按以下方式测试上面的代码:

And you can test the above code as follows:

describe('If call customPopupWidget.outOfMvp(data) function',function () {

it('it should call $ionicPopup.show().', function () {
  spyOn($ionicPopup, 'show').and.callFake(function() {
     return $q.resolve(true);
  })
  customPopupWidget.outOfMvp(data);
  expect(ionicPopup.show).toHaveBeenCalled();
});

别忘了注入$ ionicPopup,$ q否则会出错.您需要记住的一件事是您不会获得此代码的覆盖范围:

Don't forget to inject $ionicPopup, $q else you will get error. And one thing you need to remember is that you won't get coverage for this code:

onTap: function () {
   return true;
}

但是您的代码将在逻辑上完整的覆盖范围内进行测试,因为它将在点击确定"按钮时测试您想要执行的操作.

But your code will be tested with logically complete coverage as it will test what you want to do when "Ok" button is tapped.

这篇关于$ ionicPopup.show按钮onTap功能未在Angular Jasmine单元测试中获得覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆