AngularJS / Typescript/ AMD / RequireJS / ジャスミン / カルマ / シノン
ログ メッセージのプライベートな内部キューを構築する AngularJS カスタム ロギング サービスがあります。この内部キューは、キュー内のデバッグ タイプではないメッセージのみをカウントします。ある時点で、このプライベート キューを API に送信します (メッセージ サイズの制限と前述のカウントによって処理されます)。このロジックも非公開です。その http.POST をキャッチし、渡されたオブジェクトを調べて、すべてのメッセージを含むオブジェクトにデバッグが送信されていないことを確認する必要があります。
これは単体テストなので、httpBackEnd にアクセスできますが、引数を取得する方法がわかりません。私は jasmine spy や sinon も持っていますが、これまでに試した方法では結果が得られませんでした。
module MyLogClass {
export class Logger implements ng.ILogService {
private _currentSize: number = 0;
private _currentBatch: myLoggingType[] = new Array<myLoggingType>();
public totalNumberOfMessages: number = 0;
public includeDebug: boolean = false;
.
.
.
//Overriding all the Angular log functions (public functions)
//Logic for striping out debug messages if needed (private functions)
//Logic for handling when to send in here (private functions)
private SendLogs = () => {
this.http.post('someurl', (this._currentBatch))
.success( (data: any[], status, headers, config) => {
console.log('logs sent');
})
.error( (data: any[], status, headers, config) => {
//error handling call
});
}
}
}
これは、Angulars の代わりにログを記録する方法の要点を説明する簡単な宣伝文句です。そして、これが私の単体テストで達成しようとしているもののサンプルコードです。
beforeEach(angular.mock.inject(function ($http: ng.IHttpService, $httpBackend: ng.IHttpBackendService) {
localService = new MyLogService();
localService.LogHttp = $http;
httpBackend = $httpBackend;
}));
it('should strip Debug messages, then send messages and reset size', () => {
localService.totalNumberOfMessages = 50;
// Fill the Queue, make sure to put in some debug messages.
var i = 0;
// Right here I need to figure out how to catch the data sent to that URL
// The code I'm showing here is for example, I don't know what this expectPOST should look like.
httpBackend.expectPOST('someurl', data).respond('passed');
while (i < 50) {
// generating fake calls to the log service
i++;
}
httpBackend.flush();
// Check the data sent to make sure there were no debug messages in it. Only how?