3

更新:我設法通過更新httpBackend.when進一步縮小問題的範圍和解決意外的請求錯誤請求中包含一些無效的ascii字符。

我附上了一個Plunker當前代碼塊。

但是我仍然很難有效地測試成功和錯誤方法操作的觸發。我還需要增加期望來檢查消息廣播。

爲了測試上面提到的場景,我設計了一個我已經包含在Plunker中的黑客,但我相信應該有更好的方法來測試這些場景。

任何導致我如何克服這個問題並測試響應消息的狀態將不勝感激。

問題

我設置$ httpBackend的預期值/響應爲我的測試,但是當我運行$ httpBackend.flush(),它失敗指示下面提及的錯誤。

錯誤消息指出

Error: Unexpected request: GET https://192.168.1.10:3334/res?t1=CHECK&t2='?'&req={"_msgType":"Login_msg","UserName":"[email protected]","Password":"123"} 

我沒有發現有關此問題的討論領域一些有用的鏈接,他們都幫助我有所縮小問題的範圍。但我仍然無法弄清楚單元測試失敗的具體原因。

有用的堆棧溢出問題

有用的外部資源

我也試過moving the flush() call around,並且還嘗試了calling $apply or $digest,如此處所示,在$ http請求在測試中觸發。

AngularJS服務進行測試

app.service('restService',['$log','$http','$rootScope',function($log,$http,$rootScope) 
{ 
    this.location = null; 
    this.port = null; 

    this.sendRequest = function(host,port,param1,param2,requestMsg) 
    { 
     this.location = host; 
     this.port = port; 
     $http.get('https://'+host+":"+port+"/res?t1="+param1+"&t2="+param2+"&req="+JSON.stringify(requestMsg)). 
     success(function(data) { 
      //Need to add expectations to check success operations 
      $log.log('response received',data); 
      var msgData = data; 
      $rootScope.successCalled = true; 
      $rootScope.response = data; 
      if(msgData.hasOwnProperty('_msgType')) 
      { 
       $log.log('broadcast for channel',msgData._msgType); 
       $rootScope.$broadcast(msgData._msgType,msgData); 
      } 
     }). 
     error(function(){ 
      //Need to add expectations to check error method operations 
      var httpErrMsg = {_msgType:'HTTPErr', host:host, port:port, t1:param1, t2:param2, requestMsg:requestMsg}; 
      $rootScope.errorCalled = true; 
      $rootScope.response = data; 
      $rootScope.$broadcast(httpErrMsg._msgType,httpErrMsg); 
     }); 
    } 

    this.getIP = function() 
    { 
     return this.location; 
    } 
    this.getPort = function() 
    { 
     return this.port; 
    } 
}]) 

規格用於測試服務

 "use strict";         

    describe("Rest service test", function() 
    { 
     var $scope, $location, httpBackend, $log, restService , loginMsg ; 

     beforeEach(module('app')); 

    beforeEach(inject(function (_restService_) 
    { 
    restService = _restService_; 
    })); 

    beforeEach(inject(function($rootScope, _$location_, $httpBackend, _$log_) 
    { 
     $scope = $rootScope.$new(); 
     $location = _$location_; 
     httpBackend = $httpBackend; 
     $log = _$log_; 

     loginMsg={ _msgType:"Login_msg", UserName:"[email protected]", Password:"123"}; 
    })); 

    afterEach(function() 
    { 
     httpBackend.verifyNoOutstandingExpectation(); 
     httpBackend.verifyNoOutstandingRequest(); 
    }); 

    describe("Service : Rest Service", function() 
    { 
     it('Should start with location and port undefined', function() { 
     expect(restService.location).toEqual(null); 
     expect(restService.port).toEqual(null); 
     }); 

     it("check if method exists", function() { 
     expect(restService.sendRequest).toBeDefined(); 
     }); 

     it("GetIP method Exists ", function() 
     { 
     expect(restService.getIP).toBeDefined(); 
     }); 

     it("GetPort method Exists ", function() 
     { 
     expect(restService.getPort).toBeDefined(); 
     }); 

     it("GetIP Value ", function() 
     { 
     restService.location = '192.168.1.10'; 
     expect(restService.location).toEqual('192.168.1.10'); 
     }); 

     it("GetPort Value ", function() 
     { 
     restService.port = '3334'; 
     expect(restService.port).toEqual('3334'); 
     }); 

     it("Execute sendRequest()", function() { 
     httpBackend.when('GET', 'https://192.168.1.10:3334/res?t1=CHECK&t2=\'?\'&req={"_msgType":"Login_msg","UserName":"[email protected]","Password":"123"}') 
     .respond ({SessionId:"2103337586",Status:"user [email protected] not found ",EMail:"[email protected]",rejectCode:1,rejectReason:"Invalid username or password",_msgType:"LoginReply_msg"}); 
     restService.sendRequest("192.168.1.10","3334","CHECK","'?'",loginMsg); 
     httpBackend.flush(); 

     expect(restService.location).toEqual('192.168.1.10'); 
     expect(restService.port).toEqual('3334'); 
     expect($scope.successCalled).toBe(true); 
     expect($scope.response).not.toBe(null); 
     expect($scope.response._msgType).toEqual('LoginReply_msg'); 
     }); 
    }); 
}); 

回答

1

我知道,這個問題是舊的,但也許有人正在尋找,這可能是很有幫助。

修改beforeEach功能是這樣的:

beforeEach(inject(function($rootScope, _$location_, $httpBackend, _$log_) 
{ 
    $scope = $rootScope.$new(); 
    $location = _$location_; 
    httpBackend = $httpBackend; 
    $log = _$log_; 

    loginMsg={ _msgType:"Login_msg", UserName:"[email protected]", Password:"123"}; 

    httpBackend.expectGET(/192.168.1.10:3334/i).respond(function(){ 
     return [/*mockResponse*/]; 
    }); 
})); 

這將解決此錯誤。