2015-11-17 137 views
3

我使用Ember.Logger.error:測試Ember.Logger.error斷言

if (isInvalid) { 
    Ember.Logger.error('this is invalid'); 
} 

我想測試它在qunit:

assert.throws(() => myFunction(), /this is invalid/, 'error was thrown'); 

assert.throws不捕獲錯誤。如果我用簡單的throw語句替換Ember.Logger.error,確實有辦法測試記錄的Ember錯誤。任何人都知道的方式?

UPDATE:

我做了一個小插件,增加了這種能力QUnit。你可以得到它here

+0

是什麼樣的測試?它是組件,控制器,輔助單元測試嗎? –

回答

3

好了,所以我做了一個研究它是如何在灰燼做,我所看到的是,以測試它的做法:

這裏的示例測試功能可用於測試在幫助單元測試中調用Ember.Logger.error

/* global Ember */ 

import { demo } from '../../../helpers/demo'; 
import { module, test } from 'qunit'; 

module('Unit | Helper | demo'); 

test('catching log', function(assert) { 
    assert.expect(1); // define how many assertions we expect 

    const oldError = Ember.Logger.error; // store original error function in variable 

    Ember.Logger.error = function(message) { // monkey patch with our custom function 
    assert.equal(message, 'this is invalid', 'error was thrown'); // our assertion 
    }; 

    demo(); // actually call function 

    Ember.Logger.error = oldError; // restore original error function 
}); 
+0

這很好用!這有點冗長,所以我決定把它封裝在一個自定義的qunit斷言中。下一步是製作一個小插件,讓任何應用程序訪問新的qunit斷言「logs」。另外,在尋找初始化程序的等效測試時,我遇到了這個問題(https://github.com/rwjblue/ember-qunit/issues/140)。感謝您的參與! – nullnullnull

+0

世界很小。 :D –

+0

其實,在ember qunit測試中是否有與初始值設定項相同的值?據我所知,沒有。至少對於不運行'App.injectTestHelpers()'的單元測試。 – nullnullnull