我想學習如何使用茉莉花和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文件中)。
謝謝 亞歷山德拉
我看不出你在這裏發佈的代碼有什麼問題,你確定它正在加載sinon嗎?如果除了第一個('var eventSpy = sinon.spy()'),除了測試中的所有行呢? –
當我從測試中除去所提到的所有行時,出現同樣的錯誤。如果我從application.js刪除'// = require sinon.js'這一行,那麼錯誤就會變成'ReferenceError:sinon is not defined'。這意味着sinon.js在'// = require sinon.js'時被加載 - 至少可以訪問sinon對象。也許我沒有正確引用這個對象? – Alexandra
不,線路本身很好。由於在刪除sinon.js時會出現ref錯誤,這意味着在測試中,定義了「sinon」,但由於某種原因它沒有「spy」方法。是否有可能在早期的測試中爲「sinon」分配了某些東西?像是:'sinon = ...'? –