我正在寫Jasmine測試,但它顯示奇怪的行爲。用CoffeeScript代碼茉莉花測試的奇怪行爲
這是我的代碼:
root = exports ? this
class root.SomeClass
constructor: ->
@index = 0
incrementIndex: -> @index++
decrementIndex: -> @index--
,這是我的測試代碼:
describe "Object", ->
object = new SomeClass
describe ".index", ->
describe "when index = 3", ->
object.index = 3
describe "when next button is clicked", ->
object.incrementIndex()
it "returns 4", ->
expect(object.index).toBe 4
describe "when previous button is clicked", ->
object.decrementIndex()
it "returns 3", ->
expect(object.index).toBe 2
測試結果如下:
Failing 2 specs
Photos initialized .index when index = 3 when next button is clicked returns 4.
Expected 3 to be 4.
Photos initialized .index when index = 3 when previous button is clicked returns 3.
Expected 3 to be 2.
而且很奇怪的是當我註釋掉了最後4行測試代碼,測試通過。我不明白髮生了什麼...> _ <
謝謝你的幫助。
您的'describe'和'''回調是否按您認爲的順序發生?如果在「上一個按鈕」測試中放置兩個'object.decrementIndex()'調用,會發生什麼? –
消息'失敗1規格照片在下一個按鈕被點擊時索引= 3時初始化。索引返回4. 預期2返回4.'。我想'object.index'的值在兩次測試之間保存。我現在要去嘗試'beforeEach'。謝謝。 – weed