I am writing Jasmine test but it shows strange behavior.
This is my code:
root = exports ? this
class root.SomeClass
constructor: ->
@index = 0
incrementIndex: -> @index++
decrementIndex: -> @index--
And this is my test code:
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
The test result is below:
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.
And it is strange that when I comment out the last 4 lines of test code, the test pass. I could not understand what is happening… >_<
Thank you for your help.
Your tests interact which each other. Do setup in
beforeEachblocks.describe "Object", -> object = undefined beforeEach -> object = new SomeClass describe ".index", -> describe "when index = 3", -> beforeEach -> object.index = 3 describe "when next button is clicked", -> beforeEach -> object.incrementIndex() it "returns 4", -> expect(object.index).toBe 4 describe "when previous button is clicked", -> beforeEach -> object.decrementIndex() it "returns 3", -> expect(object.index).toBe 2Not checked if this piece of code is valid, but still shows how you should fix your tests. Note
object = undefinedin the 2 line. You need to declare variable here, otherwiseobjectwill be local to eachbeforeEachanditblock.