我是CoffeeScript和Jasmine的初学者。起初,我试图通过测试与下面的代码:递增测试代码应该是'++ @ count'?
class Counter
count: 0
constructor: ->
@count = 0
increment: ->
@count++
decrement: ->
@count--
reset: ->
@count = 0
root = exports ? this
root.Counter = Counter
茉莉花测试代码如下:
describe("Counter", ->
counter = new Counter
it("shold have 0 as a count variable at first", ->
expect(counter.count).toBe(0)
)
describe('increment()', ->
it("should count up from 0 to 1", ->
expect(counter.increment()).toBe(1)
)
)
)
那么善良的人告诉我,代码应该如下:
class Counter
count: 0
constructor: ->
@count = 0
increment: ->
[email protected]
decrement: ->
[email protected]
reset: ->
@count = 0
root = exports ? this
root.Counter = Counter
是的,这段代码通过了测试。但我有一个问题,即前代码比后代代码更自然。我不知道如何确定这个问题。感谢您的帮助。
你是什么意思“更自然”?如果你只看到过后面的增量,那么预先增量可能看起来是外来的,但它同样有效。 –
与你的问题有关的新问题,但'counter = new Counter'应该包含在'beforeEach'中。 – loganfsmyth