簡體   English   中英

不調用Jasmine spy 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-jasmine 0.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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM