1
我正在寫一個關於Range對象的測試。我的代碼類似於以下內容。如何測試茉莉花中的範圍對象?
Test = function() {};
Test.prototype.test = function() {
if (window.getSelection) {
var sel = window.getSelection();
var range = sel.getRangeAt(0);
// then some code;
} else if (document.selection) {
range = document.selection.createRange();
//then some code;
return 'haha';
}
it('should create range object', function() {
var myTest, result;
myTest = new Test();
spyOn(myTest, 'test').andReturn('haha');
result = myTest.test();
expect(result).toEqual('haha');
});
當我運行測試,它拋出一個錯誤:INDEX_SIZE_ERR: INDEX_SIZE_ERR: DOM Exception 1
但我的代碼工作以及在瀏覽器中。
然後我發現了一些關於getRangeAt和rangeCount。
Before the user has clicked a freshly loaded page, the rangeCount is 0.
然後我嘗試添加像$(document).click();
的代碼中,rangeCount仍然是0 ...
,它仍然拋出INDEX_SIZE_ERR: INDEX_SIZE_ERR: DOM Exception 1
所以,問題是:如何能正常測試範圍在茉莉花中的對象?
但是範圍對象是由Selection對象使用'window.getSelection()。getRangeAt(0)'創建的。這種方式不能創建一個範圍對象。對? – user2331095
範圍對象是第一個間諜返回的對象。將更新代碼以使其更清晰。 –
也許我知道你的意思。如果我需要使用像range.startOffset這樣的屬性和像range.insertNode()這樣的方法,我應該做一個假的Range對象。 'getRangeAt:function(){return {startOffset:0,insertNode:function(){return; }}}'對嗎? – user2331095