简体   繁体   中英

Testing Lodash sortBy function argument using Jasmine

I have a controller in my project that goes like this:

define(function (require) {
  'use strict';

  function AllOrgsController($rootScope, $uibModalInstance) {
    var vm = this;
    var clonedOrgs = _.cloneDeep($rootScope.userData.org);
    vm.modelContainer = _.sortBy(clonedOrgs, function (org) {
      return org.organizationName.toLowerCase();
    });

    vm.openFacilityModal = function () {
      $uibModalInstance.close();
    };

    vm.saveOrgsModal = function () {
      $uibModalInstance.close({ $value: vm.currentFacility });
    };

    vm.cancelOrgsModal = function () {
      $uibModalInstance.dismiss();
    };
  }

  AllOrgsController.$inject = ['$rootScope', '$uibModalInstance'];

  return AllOrgsController;
});

But the anonymous function being used within Lodash's _.sortBy method is not covered according to Istanbul. Since I'm a noob with unit testing I haven't figured out why – does anybody know?

我的代码中出现“功能未覆盖”错误消息

_.sortBy should call the function you passed for each element of the clonedOrgs parameter you gave it. Since Istanbul detected that the passed-in function was never run, that must mean that clonedOrgs is always empty (or not a valid array) in your tests. So you can make sure that method is covered by writing a test in which your $rootScope.userData.org array contains elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM