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 8581781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:08:04+00:00 2026-06-11T21:08:04+00:00

I am beginner of CoffeeScript and Jasmine. I tried to test a simple class

  • 0

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.

  • 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-11T21:08:06+00:00Added an answer on June 11, 2026 at 9:08 pm

    You need to use the pre-increment and pre-decrement forms if you want your increment and decrement methods to return the updated values:

    increment: -> ++@count
    decrement: -> --@count
    

    x++ yields the value of x and then increments x so this:

    return x++
    

    is equivalent to:

    y = x
    x = x + 1
    return y
    

    whereas this:

    return ++x
    

    is like this:

    x = x + 1
    return x
    

    So Jasmine is right and has nicely uncovered a bug in your code.

    For example, this code:

    class Counter
        constructor: (@count = 0) ->
        incr_post: -> @count++
        incr_pre:  -> ++@count
    
    c1 = new Counter
    c2 = new Counter    
    
    console.log(c1.incr_post())
    console.log(c2.incr_pre())
    

    will give you 0 and 1 (in that order) in the console even though @count will be 1 inside both c1 and c2 when you’re done.

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

Sidebar

Related Questions

I am beginner of CoffeeScript and Jasmine. At first, I tried to pass test
Beginner in Android development. My code crashes. I have made a simple Java method
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Beginner here! Forgive me in advance for raising what is probably an incredibly simple
Beginner here, I have a simple question. In Android what would be the best
Beginner question. I'm making a simple Gtk notepad in C# and MonoDevelop and everything
Beginner to assembly programming for x86. I have a simple asm file which I
I'm a beginner in jQuery area and I have simple question like this :
Beginner page building question: I've got a simple /div container defined via a CSS
very beginner question. I am using Rails 3's query interface as shown: class User

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.