Hi I have written a fairly trivial decorator around the '$exceptionHandler' service so I can post any failures to a http endpoint. This seems to be work fine and I'm pleased with its simplicity.
I am however having trouble testing this aspect of my application in jasmine. I have tried loading my whole app in the test i.e. 'myApp' but this to just hang the jasmine runner and crash PhantomJS & Chrome when running.
The provider:
###
* Decorate the `$exceptionHandler` providing functionality for logging
###
app.config(($provide) ->
$provide.decorator("$exceptionHandler", ($delegate, ErrorLoggingService) ->
(exception, cause) ->
# Delegate on the original `$exceptionHandler` i.e. $log.error()
$delegate(exception, cause)
# Record the error server side
ErrorLoggingService.logError(exception, cause)
)
)
describe('$exceptionHandler', function() {
beforeEach(module('myApp.exceptionHandler'));
function TestOnlyCtrl($exceptionHandler) {
$exceptionHandler("Some Random Error")
}
// Run test
describe('$exceptionHandler delegation', function(){
var $log, $httpBackend, ErrorLoggingService;
var exception = {
message: "Error Message",
stack: "Error Stack"
};
var cause = "Some Cause";
beforeEach(module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode("log");
}));
beforeEach(inject(function($injector, $controller) {
$log = $injector.get('$log');
$httpBackend = $injector.get('$httpBackend');
$exceptionHandler = $injector.get('$exceptionHandler');
$controller(TestOnlyCtrl);
ErrorLoggingService = $injector.get('ErrorLoggingService');
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
$log.reset();
});
it('Should record and delegate on to ErrorLoggingService', function() {
// I Should be able to do my asserts on the delegated service here?
expect($exceptionHandler.errors[0]).toEqual("Some Random Error");
});
});
});