1

私はこのようなテストを書きます

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-jasmine 0.9.2

4

1 に答える 1

4

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>

于 2016-05-13T10:38:50.953 に答える