我試圖通過提取出共享示例來幹掉一些茉莉花測試。Jasmine與coffeescript共享規格範圍問題
@sharedExamplesForThing = (thing) ->
beforeEach ->
@thingy = new thing
it "is neat", ->
expect(@thingy.neat).toBeTruthy()
describe "widget with shared behavior", ->
sharedExamplesForThing(-> new Widget)
當一切都在一個文件中定義時,這很好用。當我嘗試將sharedExamples移動到單獨的文件時,會遇到我遇到的問題。我得到Can't find variable: sharedExamplesForThing ...
所以在調試的利益,我試過如下:
describe "widget with shared behavior", ->
it "is acting like a meany", ->
console.log sharedExamplesForThing
expect(false).toBeTruthy()
sharedExamplesForThing(-> new Widget)
在is acting like a meany
塊,日誌顯示sharedExamplesForThing
爲[Function]
,但我仍然得到Can't find variable
的it
之外。我覺得這可能與我目前的經驗之外的範圍問題有關,但我可能完全錯誤。我在這裏錯過了什麼?
(使用導軌,jasminerice,護茉莉)
你的博客文章是非常不完整的。首先,它忽略了你正在討論的'sharedBehaviorForGameOf',其次,我有一個測試規範文件,幾乎是你的100%副本,甚至到了確切的層次結構,並且每個文件只有一個「 'describe()'塊,仍然,我的「共享」變量只對我的第一個測試可見,並且它們在第一個測試下面的任何其他測試中都是不可引用的。我認爲,如果你完成你的文章,並給出關於共享變量/函數的一些提示,像我這樣的人會很感激。除此之外,你在圖書館做得很好,我喜歡它。 – dimitarvp 2013-04-18 18:58:08
忘了提及我正在嘗試使用'runs()'塊中的共享變量,這些變量消耗了真實(非模擬)AJAX調用的結果。也許這是一個範圍問題,因爲顯然你的香草實例工作得很好。 – dimitarvp 2013-04-18 19:14:51