Karma报道中未涵盖承诺的成功功能

chi*_*rag 3 jasmine angularjs karma-jasmine

我的项目中有以下代码返回.employeeService.getJson()函数返回promise对象.承诺的成功方法不在承保范围内.

//controller code which access service method.
 export  class EmployeeCtrlClass {
   constructor(){
     employeeService.getJson()    //get promise
        .success(function (xyz) {
            employeeService.testdata = xyz.Employee;
            _this.employeeData = employeeService.testdata;
      });
   }
}
// function in Service code

 public getJson() {
        return this.$http.get('../json/TestDataJson.json'); //return promise
    }
Run Code Online (Sandbox Code Playgroud)

请在下面找到代码覆盖率的屏幕截图. 在此输入图像描述

下面的代码是针对测试用例编写的.

 it('controller constructor',() => {
     //simply i am calling constructor of controller class.
     var ctrlObj = new EmployeeCtrlClass($scope, empService);
 });
Run Code Online (Sandbox Code Playgroud)

如何在代码覆盖中涵盖此代码?谢谢.

编辑: -我已将我的测试用例更改为下面,它正在工作.

it('controller constructor',() => { 
 httpB.expectGET('../json/TestDataJson.json').respond(employeeModule.testDataJson);
 var ctrlObj = new EmployeeCtrlClass($scope, empService);
 httpB.flush();
});
Run Code Online (Sandbox Code Playgroud)

小智 6

你必须使用$ httpBackend.flush(); 进入承诺功能.如果你在describe区域中注入$ httpBackend,那应该是这样的:

var myHttpBackend;
beforeEach(inject(function($httpBackend) {
   myHttpBackend = $httpBackend;
});
 it('controller constructor',function(){
     //simply i am calling constructor of controller class.
     var ctrlObj = new EmployeeCtrlClass($scope, empService);
     myHttpBackend.flush();
 });
Run Code Online (Sandbox Code Playgroud)

我希望这能解决你的问题.

问候