Skip to content

Instantly share code, notes, and snippets.

@bbraithwaite
Last active January 24, 2017 19:24
Show Gist options
  • Save bbraithwaite/ef74e528d64a7bf1428e to your computer and use it in GitHub Desktop.
Save bbraithwaite/ef74e528d64a7bf1428e to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Log Examples</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine-html.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/boot.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-mocks.js"></script>
</head>
<body>
<script type="text/javascript">
var app = angular.module('calculatorApp', []);
app.controller('CalculatorController', function calculatorController($scope, $log) {
// We could also use:
// $log.log('standard log');
// $log.info('info log');
// $log.error('error log');
// $log.warn('warn log');
// $log.debug('some debug information');
$scope.sum = function sum() {
$log.debug('start of sum');
$scope.result = $scope.x + $scope.y;
$log.debug('the result is ' + $scope.result);
$log.debug('end of sum');
}
});
describe('calculator tests', function () {
beforeEach(module('calculatorApp'));
var $controller;
var $scope;
var $log;
describe('logs', function() {
beforeEach(inject(function(_$controller_, _$log_) {
$controller = _$controller_;
$log = _$log_;
$scope = {};
}));
it('should not call log', function() {
var productsController = $controller('CalculatorController', { $scope: $scope });
expect($log.assertEmpty).not.toThrow();
});
it('should write to debug log when calling sum', function() {
var productsController = $controller('CalculatorController', { $scope: $scope });
$scope.x = 1;
$scope.y = 2;
$scope.sum();
// we replace accordingly, e.g.
// $log.error.logs
// $log.warn.logs
expect($log.debug.logs[0]).toEqual(['start of sum']);
expect($log.debug.logs[1]).toEqual(['the result is 3']);
expect($log.debug.logs[2]).toEqual(['end of sum']);
// alternative method of checking
expect($log.debug.logs).toContain(['the result is 3']);
});
it('should not call log (using reset)', function() {
var productsController = $controller('CalculatorController', { $scope: $scope });
$scope.x = 1;
$scope.y = 2;
$scope.sum();
// this clears the logs
$log.reset();
expect($log.assertEmpty).not.toThrow();
});
});
describe('logs with debugging disabled', function () {
beforeEach(module(function($logProvider) {
// We can configure the debugging level (the default is true)
$logProvider.debugEnabled(false);
}));
beforeEach(inject(function(_$controller_, _$log_) {
$controller = _$controller_;
$log = _$log_;
$scope = {};
}));
it('should not write to log when calling sum', function() {
var productsController = $controller('CalculatorController', { $scope: $scope });
$scope.x = 1;
$scope.y = 2;
$scope.sum();
expect($log.assertEmpty).not.toThrow();
});
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment