2012-08-25 100 views
1

我想學習如何使用茉莉花和Sinon測試Backbone應用程序,並且我正在關注this tutorial。不過,我遇到了一個我不知道如何解決的問題。測試與茉莉花和Sinon骨幹模型 - 對象#<Object>沒有方法'間諜'

最有可能的解決方法很簡單,但我需要一些指導...

在我project.spec.js文件這是爲有問題的代碼:

it("should not save when name is empty", function() { 
    var eventSpy = sinon.spy(); 
    this.project.bind("error", eventSpy); 
    this.project.save({"name": ""}); 
    expect(this.eventSpy.calledOnce).toBeTruthy(); 
    expect(this.eventSpy.calledWith(
     this.project, 
     "cannot have an empty name" 
    )).toBeTruthy(); 
}); 

這是可以在瀏覽器中看到的具體錯誤:

Failing 1 spec 
7 specs | 1 failing 
Project model should not save when name is empty. 
TypeError: Object #<Object> has no method 'spy' 
TypeError: Object #<Object> has no method 'spy' 
    at null.<anonymous> (http://localhost:8888/__spec__/models/project.spec.js:53:26) 
    at jasmine.Block.execute (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:1024:15) 
    at jasmine.Queue.next_ (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2025:31) 
    at jasmine.Queue.start (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:1978:8) 
    at jasmine.Spec.execute (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2305:14) 
    at jasmine.Queue.next_ (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2025:31) 
    at onComplete (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2021:18) 
    at jasmine.Suite.finish (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2407:5) 
    at null.onComplete (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2451:10) 
    at jasmine.Queue.next_ (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2035:14) 

除了sinon.js庫,我已經安裝了茉莉花sinon.js庫(兩者在供應商/資產/ javascri pts文件夾幷包含在application.js文件中)。

謝謝 亞歷山德拉

+0

我看不出你在這裏發佈的代碼有什麼問題,你確定它正在加載sinon嗎?如果除了第一個('var eventSpy = sinon.spy()'),除了測試中的所有行呢? –

+0

當我從測試中除去所提到的所有行時,出現同樣的錯誤。如果我從application.js刪除'// = require sinon.js'這一行,那麼錯誤就會變成'ReferenceError:sinon is not defined'。這意味着sinon.js在'// = require sinon.js'時被加載 - 至少可以訪問sinon對象。也許我沒有正確引用這個對象? – Alexandra

+0

不,線路本身很好。由於在刪除sinon.js時會出現ref錯誤,這意味着在測試中,定義了「sinon」,但由於某種原因它沒有「spy」方法。是否有可能在早期的測試中爲「sinon」分配了某些東西?像是:'sinon = ...'? –

回答

2

我要張貼此作爲一個答案,基於上述評論線程。我們已經將問題範圍縮小到sinon.spy()被調用的行,所以它不是特定於此測試,而是關於如何加載sinon。

我懷疑問題是你在application.js中包含sinon和jasmine-sinon,當他們真的應該去spec/javascripts/spec.js(以相同的格式)。嘗試改變它,看看是否有任何改變。

UPDATE:

基於下面的評論線程,似乎代碼越來越爲this.project.save(...)線,但驗證不工作:我知道這是因爲,如果你在控制檯得到一個POST錯誤,這意味着骨幹實際上提出了請求(它不應該因爲名字是空的)。所以你應該回去檢查你正在測試的代碼。

+0

在創建包含行的spec.js文件後// // require require應用程序 // = require_tree。// =需要jasmine-sinon // =需要sinon',將spec/javascripts下的helpers文件夾中的sinon.js和jasmine-sinon.js文件移動,當我試着console.log(sinon)包含間諜功能。不過,我仍然得到一個錯誤:'TypeError:sinon.spy未定義。 var _invoke = sinon.spy.invoke;'任何額外的想法?順便說一句,非常感謝你的幫助! – Alexandra

+0

spec.js中的順序應該是:require_tree ./,需要sinon,需要jasmine-sinon,需要應用程序(至少這就是我的文件中的內容)。您也可以將sinon和jasmine-sinon放在vendor/assets/javascripts文件夾中,這是沒有問題的。 –

+0

不幸的是,訂單沒有修復錯誤..還有一個POST錯誤。響應是「找不到文件:/ projects」。 – Alexandra

6

當我從GitHub(沒有Sinon文件夾)下載sinon.js文件時,我遇到了這個問題。我通過從http://sinonjs.org/

+1

確認這解決了我的問題 - 謝謝,救了我頭痛 – cantera

+0

我有一個類似的問題(鮑爾,amd,摩卡,sinon)與sinon.stub(myObject,aMethod),當我從一個下載文件(從http://sinonjs.org)到涼亭安裝包。我試圖要求,填充stub.js(spy.js和behavior.js)的依賴關係,但放棄了。恢復了下載的sinon-1.9.0.js,並且一切都恢復正常。我確信我不正確地「需要」模塊 - 我確切的問題是存根方法返回了一個似乎是間諜,而不是存根(我需要調用mystub。返回(true)它不會返回代理上的任何方法)。這是否有意義? – gumaflux

+0

Ehhm剛剛意識到我缺少call.js包含在所需的模塊中,現在它工作hunky dory .. :) – gumaflux

0

下載庫解決了這個問題我知道這個線程是舊的,但我今天遇到類似的問題時,通過本教程http://tinnedfruit.com/2011/03/25/testing-backbone-apps-with-jasmine-sinon-2.html。它看起來像Backbone做出了改變,當提供無效的模型數據時,它會調用'invalid'事件,而不是'錯誤'。

如果您遇到這樣的錯誤嘗試改變:

it("should not save when name is empty", function() { 
    ... 
    this.project.bind("error", eventSpy); 
    ... 
}); 

要:

it("should not save when name is empty", function() { 
    ...   
    this.project.bind("invalid", eventSpy); 
    ... 
}); 

這解決了這個問題對我來說。