2013-02-12 73 views
2

我是Jasmine和jQuery的新手,並且在mousedown事件中遇到了麻煩。這是我規範中失敗的部分(Jasmine說,間諜mousedown沒有被調用。)我懷疑這可能是因爲我觸發事件的方式。茉莉花鼠標事件

it("draws a path on mousedown", function() { 
    spyOn(drawing,'mousedown'); 
    $('#canvas').trigger('mousedown'); 
    expect(drawing.mousedown).toHaveBeenCalled(); 
    expect(drawing.isDrawing).toEqual(true); 
}); 

相應的JavaScript代碼是在這裏:

function Drawing(canvas, eraseAllButton, eraseButton) { 
    this.isDrawing = false; 
    this.erasing = false; 
    this.canvas = canvas; 
    this.context = canvas.getContext("2d"); 
    this.eraseAllButton = eraseAllButton; 
    this.eraseButton = eraseButton; 

// Sets up event listeners for drawing. 
    var self = this; 
    this.canvas.addEventListener("mousedown", function() { self.mousedown() }, false); 
}; 

// Begins to draw a path on mousedown. 
Drawing.prototype.mousedown = function(e) { 
    if (!e) var e = window.event; 
    e.preventDefault();  

    this.isDrawing = true; 
    this.x = e.pageX - this.canvas.offsetLeft; 
    this.y = e.pageY - this.canvas.offsetTop; 
    this.context.beginPath(); 
    this.context.moveTo(this.x, this.y); 
}; 
+0

是全規格可用的地方,所以我們可以看到你的beforeEach等?也許創建一個JSFiddle? – 2013-04-09 02:23:33

回答

0

試試這個:

var mouse = document.createEvent('MouseEvents'); 
mouse.initMouseEvent('mousedown', true, true, window); 
$('#canvas').get(0).dispatchEvent(mouse); 
+0

我們如何在jQuery中做到這一點? – sonam 2013-09-20 12:40:55