I found a way, by using $provide to provide some mock services and
constants, in my cas it is ngToast and config (contains the constants in
the real app). Hope it can help someone
describe('Unit: Exception handler', function() {
var $exceptionHandler, logger, $rootScope;
//html templates
beforeEach(module('templates'));
//load main module and mock translations
beforeEach(module('app.common', function($provide, $exceptionHandlerProvider)
{
$exceptionHandlerProvider.mode("log");
$provide.factory('ngToast', function () {
return {
create: function() {}
};
});
$provide.constant('config', {});
}));
beforeEach(inject(function (_$rootScope_, _$exceptionHandler_, _logger_) {
$rootScope = _$rootScope_;
$exceptionHandler = _$exceptionHandler_;
logger = _logger_;
spyOn(logger, 'logError');
}));
it('init the data page', function() {
$exceptionHandler("Some Random Error");
expect(logger.logError).toHaveBeenCalled();
});
});
Le mardi 18 mars 2014 10:00:34 UTC, James Morgan a écrit :
>
> 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.
>
> Any ideas how I can test this code, ideally in isolation?
>
> Thanks for any tips,
>
> James
>
> 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)
> )
> )
>
>
> The test so far:
>
> 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");
>
> });
> });
> });
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.