Cố gắng kiểm tra xem trình xử lý sự kiện có được gọi trên phần tử được nhấp với Jasmine hay không. Có đối tượng "Pad" chứa phần tử DOM "PadElement", được nhấp vào. Xử lý sự kiện là một phương pháp trên đối tượng Pad:Jasmine không thể gián điệp xử lý sự kiện?
GRAPH.Pad = function(graphDiv, graph) {
this.graph = graph;
this.clickHandler = function(e) {
console.log('padElement clickHandler called');
//this.graph.createVertex(e.clientX, e.clientY);
};
this.padElement = GRAPH.padElement(graphDiv, this.clickHandler);
}
GRAPH.padElement = function(graphDiv, clickHandler) {
//Initialize pad
var NS="http://www.w3.org/2000/svg";
var pad=document.createElementNS(NS,"svg");
pad.setAttributeNS(null, 'id', 'pad');
graphDiv.appendChild(pad);
pad.addEventListener('click', clickHandler)
return pad;
}
Các thử nghiệm Jasmine:
var testDiv = document.createElement('div');
var testGraph = new GRAPH.Graph(testDiv);
var testPad = new GRAPH.Pad(testDiv, testGraph);
it('has its clickHandler function called when its padElement is clicked',
function() {
spyOn(testPad, "clickHandler");
simulateClick(testPad.padElement);
//testPad.clickHandler();
expect(testPad.clickHandler).toHaveBeenCalled();
});
Tuy nhiên, thử nghiệm thất bại. Lưu ý rằng trình nghe sự kiện không được gọi (console.log viết thành công với một cú click chuột và với simulateClick), VÀ nếu tôi chỉ gọi testPad.clickHandler() trực tiếp của điệp viên của Jasmine có thể nhặt nó lên. Nhưng điều gì sẽ xảy ra trong quá trình kiểm tra thực tế? Liệu trình xử lý sự kiện có được chuyển đến một đối tượng khác trong thời gian chạy không? Cách thích hợp để làm điều này là gì?