【发布时间】:2012-12-04 05:26:43
【问题描述】:
我正在编写 Jasmine 测试,但它显示出奇怪的行为。
这是我的代码:
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和it回调是否按照您认为的顺序发生?如果您在“上一个按钮”测试中放置两个object.decrementIndex()调用会发生什么? -
邮件
Failing 1 spec Photos initialized .index when index = 3 when next button is clicked returns 4. Expected 2 to be 4.已返回。我想object.index的值保存在两个测试之间。我现在要试试beforeEach。谢谢。
标签: coffeescript jasmine