3

我正在使用 Ember.Logger.error:

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

我想在 qunit 中测试它:

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

assert.throws没有捕捉到错误。如果我Ember.Logger.error用一个简单的throw语句替换它就可以了,但肯定有一种方法可以测试记录的 Ember 错误。有人知道路吗?

更新:

我做了一个小插件,将这种能力添加到 QUnit。你可以在这里得到它。

4

1 回答 1

3

好的,所以我已经研究了它是如何在 Ember 中完成的,并且我已经看到了测试它的做法是什么:

这是可用于测试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
});
于 2015-11-21T13:40:21.550 回答