我是 CoffeeScript 和 Jasmine 的初学者。起初,我尝试使用以下代码通过测试:
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: ->
++@count
decrement: ->
--@count
reset: ->
@count = 0
root = exports ? this
root.Counter = Counter
是的,这段代码通过了测试。但我有一个问题,前一个代码比后一个代码更自然。我不知道如何确定这个问题。谢谢您的帮助。