2012-06-19 27 views
8

我試圖調試窺視jQuery.post不點火,這樣一個全面的檢查,我想茉莉間諜爲什麼不認爲它被稱爲即使它返回和返回值?

spyOn(this.viewModel.requests, 'submitRequest').andReturn('fooz'); 

var ret = this.viewModel.requests.submitRequest(); 
expect(ret).toEqual('foo'); 

expect(this.viewModel.requests.submitRequest).toHaveBeenCalled(); 

這種失敗

預計「福茲」平等「富」。

但是,當我在參數改變'fooz''foo'andReturn,測試失敗與submitRequest

預計間諜已被調用。

間諜正在返回罐裝值,那麼爲什麼toHaveBeenCalled失敗?

+0

你茉莉花的版本是? – Dancrumb

+0

@Dancrumb 1.2.0-rc3 –

回答

1

我知道這不應該是解決方案,但你嘗試過

var submitSpy = spyOn(this.viewModel.requests, 'submitRequest').andReturn('foo'); 

var ret = this.viewModel.requests.submitRequest(); 
expect(ret).toEqual('foo'); 

expect(submitSpy).toHaveBeenCalled(); 

因爲有時候這工作更穩定

+0

感謝您的建議,但是這個測試也失敗了:「期待間諜submitRequest被調用。」 –

+0

@GregBacon您是否嘗試過使用console.log(submitSpy)?你會得到什麼?這真的很奇怪,你在使用什麼框架?Backbone? –

1

你的代碼應該工作。我已經測試過它的茉莉獨立的例子:

it("tells the current song if the user has made it a favorite", function() { 
    spyOn(song, 'persistFavoriteStatus').andReturn('foo'); 
    var ret = song.persistFavoriteStatus(); 
    expect(ret).toEqual('foo'); 

    expect(song.persistFavoriteStatus).toHaveBeenCalled(); 
}); 

我的直覺告訴我,你所遇到的問題與之前茉莉花的範圍有關/來電之後 - 我碰到令人沮喪的情況下,像我自己。在測試開始時,我會檢查以確保環境符合您的預期(例如,間諜會被重置)。

相關問題