简体   繁体   English

覆盖 Console.log

[英]Overriding Console.log

Sup guys, big wall of code incoming伙计们,一大堆代码来了

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();

So I'm studying to take the Hack Reactor entrance exam and I'm on module 2 right now, which focuses on testing functions.所以我正在学习参加 Hack Reactor 入学考试,我现在正在学习模块 2,该模块侧重于测试功能。 This is a reference solution for applying an assertObjectsEqual testing function.这是应用 assertObjectsEqual 测试功能的参考解决方案。 What I'm confused about is in the testFailureCaseAssertObjectsEqual function lines:我感到困惑的是在 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;

I don't really understand what's happening here.我真的不明白这里发生了什么。 What does overriding the console logger to trap messages mean and why do we do it?覆盖控制台记录器以捕获消息是什么意思,我们为什么要这样做? Also how does the code above accomplish that?另外上面的代码是如何实现的? Sorry if my post contains a lot of unnecessary info, idk what is and isn't directly relevant to my question so I included all of it.抱歉,如果我的帖子包含很多不必要的信息,请指出与我的问题直接相关和不相关的内容,因此我包含了所有内容。 Thank you for taking the time.感谢您抽出宝贵时间。

They are wanting to intercept the first argument of console.log temporarily for this one use case and not have it printed to console他们想为这个用例临时拦截 console.log 的第一个参数,而不是将它打印到控制台

This is accomplished by storing a reference to original function and assigning a different function to console.log.这是通过存储对原始函数的引用并将不同的函数分配给 console.log 来实现的。

Then after they are done - make console.log do what it is normally supposed to do by reassigning the original function to it again然后在它们完成后 - 通过再次将原始函数重新分配给它,使 console.log 做它通常应该做的事情

Following simplified demo and comments should help understand process better遵循简化的演示和评论应该有助于更好地理解过程

 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