业力茉莉花底层对象调用失败

时间:2020-02-25 21:09:45

标签: mocking karma-jasmine

我是茉莉花测试的新手,并且在更深层的函数调用中通过调用获得此错误: ReferenceError:未定义foo

这是我的控制器代码:

var resultCallBack = {
   setValue: function (val) {
      if(val === foo.Code.HELP){
          $scope.msg = 'Help is here'
      } else if (val === foo.Code.ERROR){
  $scope.msg = 'ERROR!'
} else {
  $scope.msg = 'SUCCESS!'
}
   }
}

$http({
  method: 'post',
  url: '/api/activate',
  data: {a: 'a', b: 'b'}

}).then(function(result){
  if(result.data.bar === 'yes') {
    $scope.display(result.data, resultCallBack)
  } else {
    resultCallBack.setValue('ERROR')
  }
}

这是我的考试:

describe('when http post happens', function() {
  beforeEach(function ($injector)) {
     this.$http = $injector.get('$http');
this.$httpBackend = $injector.get('$httpBackend');
  }

 it('post is success', function(){

   var msg;
   var test = {a: 'a', b: 'b'}
   var resultCallBack = {
       setValue: function(value) {
          msg = value;
       }
   }

   this.$controller('myController', {$scope: this.$scope, $http: this.$http});
  this.$httpBackend
      .whenPOST('/api/activate')
      .respond(200, test);
  this.$httpBackend.flush();
  expect.......
}

);

控制器开始http调用,我正在尝试g测试成功和错误的情况,但是我似乎无法动手模拟resultCallBack,因为它总是抛出foo是不确定的。 foo是html页面中包含的外部库引用。在通话成功后,如何针对每种情况正确测试http通话?谢谢

0 个答案:

没有答案