覆盖Console.log

时间:2018-08-04 09:08:07

标签: javascript override console.log

伙计们,大量的代码传入

function assertObjectsEqual(actual, expected, testName) {
  actual = JSON.stringify(actual);
  expected = JSON.stringify(expected);
  if ( actual === expected ) {
    console.log('PASSED [' + testName + ']');
  } else {
    console.log('FAILED [' + testName + '], expected "' + expected + '", but got "' + actual + '"')
  }
}

/////////////////////////////////
// Tests for "assertObjectsEqual"
/////////////////////////////////

// Note: testing assertion functions is a bit of "Inception"...
// You have to use an assert to test your other assert.
// And since the output is console-logged, you have to trap that too.
// Hence I don't think it's reasonable to expect students to do the negative test on these.
// I think it's sufficient for students to just console log what these assertions do in the failure cases
// and move on...
// But just for illustration, here it is:
function assert(actual, testName) {
  if ( actual !== true ) {
    console.log('FAILED [' + testName + '] Expected "' + expected + '" to be true.');
  } else {
    console.log('PASSED [' + testName + ']');
  }
}

function testFailureCaseAssertObjectsEqual() {
  var objectA = {a: 1};
  var objectB = {b: 2};
  var originalConsoleLog = console.log; // we're going to override console logger to be able to trap messages.

  var trappedMsg;
  console.log = function(msg) {
    trappedMsg = msg; // trap the message via a closure
  }
  assertObjectsEqual(objectA, objectB);

  console.log = originalConsoleLog; // restore the mocked logger before doing our real assertion
  assert(trappedMsg.includes('FAILED'), 'should fail when given two different objects');
}
testFailureCaseAssertObjectsEqual();

因此,我正在研究参加Hack Reactor入学考试,并且我现在正在学习模块2,该模块侧重于测试功能。这是应用assertObjectsEqual测试函数的参考解决方案。我感到困惑的是在testFailureCaseAssertObjectsEqual函数行中:

var originalConsoleLog = console.log; // we're going to override console logger to be able to trap messages.

  var trappedMsg;
  console.log = function(msg) {
    trappedMsg = msg; // trap the message via a closure
  }
  assertObjectsEqual(objectA, objectB);

  console.log = originalConsoleLog;

我不太了解这里发生了什么。覆盖控制台记录器以捕获消息是什么意思,为什么我们要这样做?另外,上面的代码如何做到这一点?抱歉,如果我的帖子包含很多不必要的信息,则idk是什么与我的问题没有直接关系,因此我将所有内容都包括在内。谢谢您抽出宝贵的时间。

1 个答案:

答案 0 :(得分:1)

对于这种用例,他们希望暂时拦截console.log的第一个参数,而不是将其打印到控制台

这是通过存储对原始功能的引用并将不同的功能分配给console.log来实现的。

然后,在完成后-通过再次将原始功能重新分配给console.log来执行通常应做的事情

下面的简化演示和评论应有助于更好地了解流程

function doLog(msg){
   console.log(msg)
}

function myTest() {

  // store reference to console.log function
  var originalConsoleLog = console.log;

  // temporarily make console.log do something different than logging to console 
  // by assigning new function to it
  console.log = function(msg) {
    alert('Intercepted: ' + msg)
  }
  
  // anywhere in here if console.log gets used it will do above alert   
  doLog('First message');/* log gets called inside this function so will alert instead*/
  
  console.log('Second message');/* will also alert and not print in console */
  
  // override finished, return it to do what it normally does 
  // by reassigning it's own original function
  console.log = originalConsoleLog;
  
  // use console.log again and it will do what it normally does
  doLog('Third message should appear normally in console')
  
}

myTest();

// proceed as if nothing was ever different
doLog('Forth message - back to normal')

相关问题