0

I'm having issues with ngMocks and the $log provider that is mocked in there.

Anytime a use $log in my code, the tests fails, because of the mock for $log fail at (line 295):

var $log = {
  log: function() { $log.log.logs.push(concat([], arguments, 0)); },
  warn: function() { $log.warn.logs.push(concat([], arguments, 0)); },
  info: function() { $log.info.logs.push(concat([], arguments, 0)); },
  error: function() { $log.error.logs.push(concat([], arguments, 0)); },
  debug: function() {
    if (debug) {
      $log.debug.logs.push(concat([], arguments, 0));
    }
  }
};

as $log.info.logs looks to be undefined and not an array.

I've noticed that changing the log function in ngMock in this way:

info: function() { 
    $log.info.logs = $log.info.logs || [];
    $log.info.logs.push(concat([], arguments, 0)); 
  },

make my test pass.

Any idea on why this can happen? I think it's not a bug in ngMock as I've not found any reference around.

teone
  • 2,153
  • 3
  • 25
  • 49

1 Answers1

3

Same issue happening here.

It started to happen once I added a decorator on $log to interpect debug calls. It happen if you call log debug in a initialization phase of any angular component only.

I workaround it checking if the mock implementation is in place (in jasmine tests) and calling reset to create the array expected.

$provide.decorator('$log', function ($delegate) {
    // Keep track of the original debug method, we'll need it later.
    var debugFn = $delegate.debug;
    /*
     * Intercept the call to $log.debug() so we can add on
     * our enhancement. We're going to add on a date and
     * time stamp to the message that will be logged.
     */
    $delegate.debug = function () {

        var args = [].slice.call(arguments);
        args[0] = ['Debug', ' - ', new Date().toString(), ': ', args[0]].join('');

        // HACK awfull fix for angular mock implementation whithin jasmine test failing issue
        if (typeof $delegate.reset === 'function' && !($delegate.debug.logs instanceof Array)) {
          // if we are within the mock and did not reset yet, we call it to avoid issue
          // console.log('mock log impl fix to avoid logs array not existing...');
          $delegate.reset();
        }

        // Send on our enhanced message to the original debug method.
        debugFn.apply(null, arguments);
    };

    return $delegate;
});

Hope this helps...

Wolfium
  • 126
  • 1
  • 4