Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9002825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:37:21+00:00 2026-06-16T00:37:21+00:00

I am writing Jasmine test but it shows strange behavior. This is my code:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:37:22+00:00Added an answer on June 16, 2026 at 12:37 am

    Your tests interact which each other. Do setup in beforeEach blocks.

    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 2
    

    Not checked if this piece of code is valid, but still shows how you should fix your tests. Note object = undefined in the 2 line. You need to declare variable here, otherwise object will be local to each beforeEach and it block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a mock for the node-redis module and using Jasmine to test
Writing some XML documentation for a predicate helper class. But I can't figure out
I am writing unit test using Jasmine in a web app using BackboneJS. There
I have been writing tests for my Ruby code for a while, but as
I'm writing specs for different test cases for Jasmine and QUnit to compare them
I'm writing a Rails app which has the following jasmine spec: describe buttons, ->
Writing Classic ASP code in JScript has a lot going for it: more humane
Writing htaccess that allows me to remove index.php from the URL can confuse search
Writing a test app to emulate PIO lines, I have a very simple Python/Tk
Writing documentation in html requires some code examples. What to do with characters that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.