1

AngularJS / Typescript/ AMD / RequireJS / Jasmine / Karma / Sinon

I have an AngularJS custom logging service that builds a private internal queue of log messages. This internal queue only counts messages that are not of type debug in its queue. At some point it will send this private queue to an API (handled by a message size limit and the count previously mentioned). This logic is also private. I need to catch that http.POST and examine the object passed to it to make sure that no debugs were sent in the object that contains all the messages.

This is unit testing, so I have access to httpBackEnd, but I've not clue how to get at the args. I also have jasmine spy's, and even sinon, but none of the methods I've tried so far have yielded results.

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
            });
     }
  }
}

So that's a quick blurb that describes the gist of my logging replacement for Angulars. And here's some sample code of what I'm trying to achieve with my Unit Test.

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?
Paul
  • 11
  • 2

0 Answers0