2016-05-13 45 views
1

我寫這樣茉莉花間諜document.execCommand不叫

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

回答

2

茉莉花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>