首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >覆盖Console.log

覆盖Console.log
EN

Stack Overflow用户
提问于 2018-08-04 17:08:07
回答 1查看 661关注 0票数 2

超级大哥,一大堆代码进来了

代码语言:javascript
复制
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函数行:

代码语言:javascript
复制
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;

我真的不明白这里发生了什么。覆盖控制台记录器来捕获消息意味着什么?我们为什么要这样做?另外,上面的代码是如何做到这一点的?抱歉,如果我的帖子包含了很多不必要的信息,我知道什么是和我的问题不直接相关的,所以我把它们都包括进来了。谢谢你抽出时间来。

EN

回答 1

Stack Overflow用户

发布于 2018-08-04 18:04:19

他们希望暂时截取此用例的console.log的第一个参数,而不将其打印到控制台

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

然后,在它们完成后-通过再次将原始函数重新分配给console.log,让它执行它通常应该做的事情

遵循简化的演示和评论应该有助于更好地理解流程

代码语言:javascript
复制
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')

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51684115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档