I am beginner of CoffeeScript and Jasmine. I tried to test a simple class as below:
class Counter
count: 0
constructor: ->
@count = 0
increment: ->
@count++
decrement: ->
@count--
reset: ->
@count = 0
root = exports ? this
root.Counter = Counter
Then, I wrote test code as below:
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)
)
)
)
The second test always failed and the message was below:
Expected 0 to be 1.
Thank you for your kindness.
You need to use the pre-increment and pre-decrement forms if you want your
incrementanddecrementmethods to return the updated values:x++yields the value ofxand then incrementsxso this:is equivalent to:
whereas this:
is like this:
So Jasmine is right and has nicely uncovered a bug in your code.
For example, this code:
will give you
0and1(in that order) in the console even though@countwill be1inside bothc1andc2when you’re done.