繁体   English   中英

覆盖 Console.log

[英]Overriding 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;

我真的不明白这里发生了什么。 覆盖控制台记录器以捕获消息是什么意思,我们为什么要这样做? 另外上面的代码是如何实现的? 抱歉,如果我的帖子包含很多不必要的信息,请指出与我的问题直接相关和不相关的内容,因此我包含了所有内容。 感谢您抽出宝贵时间。

他们想为这个用例临时拦截 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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM