更新:無効な ASCII 文字を含むhttpBackend.when要求を更新することで、問題の範囲をさらに絞り込み、予期しない要求エラー
現在のコード ブロックにPlunkerを添付しました。
しかし、成功とエラーのメソッド操作のトリガーを効果的にテストすることはまだ困難です。また、メッセージのブロードキャストをチェックするための期待値を追加する必要があります。
上記のシナリオをテストするために、Plunker に含めたハックを考案しましたが、これらのシナリオをテストするにはもっと良い方法があるはずだと思います。
この問題を克服し、応答メッセージのステータスをテストする方法についてのリードをいただければ幸いです。
問題
テスト用に $httpBackend の期待値/応答を設定しましたが、$httpBackend.flush() を実行すると失敗し、以下のエラーが表示されます。
エラーメッセージが表示されました
Error: Unexpected request: GET https://192.168.1.10:3334/res?t1=CHECK&t2='?'&req={"_msgType":"Login_msg","UserName":"e@e.com","Password":"123"}
この問題のドメインについて説明している便利なリンクをいくつか見つけましたが、問題の範囲を絞り込むのにいくらか役立ちました。しかし、この単体テストが失敗する具体的な理由はまだわかりません。
役に立つスタック オーバーフローの質問
- Angular モック $httpBackend は、フラッシュする保留中のリクエストはありません
- $httpBackend で AngularJS を単体テストすると、「エラー: 予期しない要求」が返されます</a>
- 外部サービスで Angular $resource をテストする
- エラーが表示されるのはなぜですか … 予期しない要求
- $httpBackend のモック
役立つ外部リソース
- HTTP モックを使用した AngularJS テスト
- Angular.js での $http サービスの単体テスト
- Angularjs チュートリアル: ngMock $httpBackend と karma を使用したテスト。
- Angular JS - 単体テスト - サービス
- HTTP モックを使用した AngularJS テスト
また、ここで提案されているように、テストで $http リクエストを起動するときに、flush() 呼び出しを移動して、$apply または $digest を呼び出してみました。
テストする 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:"e@e.com", 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":"e@e.com","Password":"123"}')
.respond ({SessionId:"2103337586",Status:"user e@e.com not found ",EMail:"e@e.com",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');
});
});
});