AngularJS / Typescript/ AMD / RequireJS / Jasmine / Karma / Sinon
我有一个 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?