我是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