[英]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.