0

我遇到了问题,ngMocks并且$log在那里被嘲笑的提供者。

任何时候$log在我的代码中使用,测试都会失败,因为模拟$log失败(第 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看起来是未定义的,而不是数组。

我注意到ngMock以这种方式更改日志功能:

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

让我的测试通过。

知道为什么会发生这种情况吗?我认为这不是一个错误,ngMock因为我没有找到任何参考。

4

1 回答 1

3

同样的问题发生在这里。

一旦我在 $log 上添加了一个装饰器来拦截调试调用,它就开始发生了。如果您仅在任何角度组件的初始化阶段调用日志调试,就会发生这种情况。

我解决它检查模拟实现是否到位(在茉莉花测试中)并调用重置以创建预期的数组。

$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;
});

希望这可以帮助...

于 2016-05-12T14:45:39.100 回答