[英]Ember how to test my console.log wrapper?
我有一個包裝console.log的日志服務。 我很難測試它,因為console.log是異步的。 我覆蓋了console.log,但是仍然存在異步問題。 在我的斷言之后調用console.log函數。
wrapper(msg) {
console.log.bind(console); //I am binding it because in my real code the console is looked up in a map.
console.log(msg);
}
test('log() can handle valid mask', function (assert) {
let passed = false;
subject = this.subject();
console.log = function() {
passed = true;
};
subject.wrapper('testing');
assert.equal(passed, true);
});
如何獲取它以等待console.log運行? 我嘗試使用諾言,但是我沒有運氣。
您可以從qunit使用異步
test('log() can handle valid mask', function (assert) {
let passed = false;
debugger;
console.log = function() {
setTimeout(function() {
passed = true;
}, 0);
};
var done1 = assert.async();
wrapper('testing');
setTimeout(function() {
assert.equal(passed, true);
done1();
}, 100);
});
http://jsbin.com/nozoruxori/edit?js,輸出正常
使用Ember進行異步檢查,可以使用andThen
包裝器(用於異步聲明)
也看起來像console.log.bind(console); //I am binding it because in my real code the console is looked up in a map.
console.log.bind(console); //I am binding it because in my real code the console is looked up in a map.
在這種狀態下錯過某件事是沒有意義的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.