茉莉花间谍document.execCommand不叫

Jasmine spy document.execCommand is not called

我这样写测试

describe('execCommand', function () {
    it('should call document.execCommand', function () {
        spyOn(document, 'execCommand').and.callThrough();
        expect(document.execCommand).toHaveBeenCalledWith('foreColor', false, 'red');
        document.execCommand('foreColor', false, 'red');
    });
});

但是它失败了 Expected spy execCommand to have been called with [ 'foreColor', false, 'red' ] but it was never called. 我不知道为什么?

请帮忙。

注意:我运行它使用grunt-contrib-jasmine0.9.2

Jasmine 的 expect 函数是当时的断言,而不是 it 块末尾将 运行 的断言,切换顺序以查看它的工作:

describe('execCommand', function () {
  it('should call document.execCommand', function () {
    spyOn(document, 'execCommand').and.callThrough();
    document.execCommand('foreColor', false, 'red');
    expect(document.execCommand).toHaveBeenCalledWith('foreColor', false, 'red');
  });
});
<link href="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine.css" rel="stylesheet" />
<script src="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine-2.0.3-concated.js"></script>