我有一个AngularJS自定义日志记录服务,用于构建日志消息的私有内部队列。此内部队列仅计算其队列中不属于debug类型的消息。在某些时候,它会将此私有队列发送到API(由消息大小限制和前面提到的计数处理)。这个逻辑也是私人的。我需要捕获http.POST并检查传递给它的对象,以确保在包含所有消息的对象中没有发送任何调试。
这是单元测试,所以我可以访问httpBackEnd,但我不知道如何获得args。我也有茉莉花间谍,甚至是僧星,但到目前为止我尝试过的方法都没有产生结果。
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?