Why is $ log always disabled using angular-mocks?

Service

$ log is recommended for console.log for AngularJS applications. One common use case for such logs is to view debugging prints when performing tests. The problem is that angular can silence $ log by default, replacing it with a layout. Well, I sometimes need to test my debug printing, but I just need to see it much more often. The problem is that the default behavior insists on using a dummy log, and I don't even see the right way to get back to the real $ log. I made a jsfiddle example to illustrate it, try running it looking at the devtools console http://jsfiddle.net/ivan4th/EnvL9/

var myApp = angular.module('myApp', []);

describe('myApp', function () {
    var element, rootScope;
    beforeEach(module('myApp'));
    it('does something', inject(function ($log) {
        $log.log("this message gets eaten by angular-mocks");
        console.log("this message is visible though");
    }));
});

The first message is skipped, and the second is displayed as expected. Why is this strange behavior used, and is there a way to fix it without using $ log?

+4
source share
2 answers

Based on the idea of ​​dtabuenc's answer, I applied a workaround that dumped logs for Jasmine's tests that fail, in the form of global afterEach ():

afterEach(inject(function ($log) {
  // dump log output in case of test failure
  if (this.results().failedCount) {
    var out = [];
    angular.forEach(["log", "info", "warn", "error", "debug"], function (logLevel) {
      var logs = $log[logLevel].logs;
      if (!logs.length)
        return;
      out.push(["*** " + logLevel + " ***"]);
      out.push.apply(out, logs);
      out.push(["*** /" + logLevel + " ***"]);
    });
    if (out.length) {
      console.log("*** logs for: " + this.description + " ***");
      angular.forEach(out, function (items) { console.log.apply(console, items); });
    }
  }
  $log.reset();
}));

Unfortunately, without replacing the angular -mocks' $ log implementation, the order of messages with different levels of the log is lost, but I think I can live with it. Disabling all log messages, including Angular's own errors and warnings during tests, still sounds like a bad design choice, though.

+2
source

mock $log , , , , .

, , :

$log.log = function(message){ console.log(message);};

, :

console.log($log.log.logs)

.

+5

All Articles