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

  • Home
  • SEARCH
  • 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 7497291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:01:17+00:00 2026-05-29T19:01:17+00:00

Say I have a class in Coffeescript: class MyGame constructor: () -> @me =

  • 0

Say I have a class in Coffeescript:

class MyGame
   constructor: () ->
      @me = new Player
      @opponents = [new Player, new Player]

which would like to test in Jasmine:

describe "MyGame", ->
   beforeEach ->
     window.game = new MyGame

   it "should have two players", ->
      expect(window.game.opponents.length).toEqual 2

But I get the error TypeError: Result of expression 'window.game.opponents' [undefined] is not an object.?

The window.game approach also seem awkward to me. If I try to define it as @game = new MyGame I get the error ReferenceError: Can't find variable: MyGame but I guess that has something to do with the way Coffeescript is wrapping things up?

UPDATE: The problem seems more like a reference problem as described above. I’m running with guard-jasmine which looks like

guard 'jasmine', :all_on_start => false, :all_after_pass => false do
  watch(%r{app/assets/javascripts/(.+)\.(js\.coffee|js)}) { |m| "spec/javascripts/#{m[1]}_spec.#{m[2]}" }
  watch(%r{spec/javascripts/(.+)_spec\.(js\.coffee|js)})  { |m| "spec/javascripts/#{m[1]}_spec.#{m[2]}" }
  watch(%r{spec/javascripts/spec\.(js\.coffee|js)})       { "spec/javascripts" }
end

and my jasmine.yml file has:

src_files:
    - "app/assets/**/*.js"
    - "app/assets/**/*.coffee"
spec_files: 
    - '**/*[sS]pec.js.coffee' 
asset_pipeline_paths: 
    - app/assets 
    - spec/javascripts

I get the an ReferenceError: Can't find variable: MyGame so I figure it’s either something with the Rails 3.1 asset pipeline or the way Coffeescript wraps objects.

  • 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-05-29T19:01:18+00:00Added an answer on May 29, 2026 at 7:01 pm

    try defining your coffeescript class using the @ operator as such:

    class @MyGame
       constructor: () ->
          @me = new Player
          @opponents = [new Player, new Player]
    

    this will allow you to access the class from anywhere, such as from your jasmine tests, and also you can get away from attaching testing variables to window:

    describe "MyGame", ->
       beforeEach ->
         @game = new MyGame
    
       it "should have two players", ->
          expect(@game.opponents.length).toEqual 2
    

    the reason for this is that coffeescript goes out of its way to avoid introducing global variables by wrapping everything in a closure. unfortunately, this can be undesirable for object-oriented code. using the @ operator attaches the class definition to the global this, which is window, and thus allows you to instantiate your classes as you like. you may have some global vars in your global space now, your classes, but for me its an ok trade-off. hope this helps!

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

Sidebar

Related Questions

Here is what I would like to do. Say I have Class Widget. I
say I have: class Test { public static int Hello = 5; } This
Say I have a class named Frog, it looks like: public class Frog {
Say I have a class called PermissionManager which should only exist once for my
Say I have a class that looks like the following: internal class SomeClass {
Say I have a class that looks something like this: class SomeClass { int
Let's say I have class PersonSummary, which combines properties (columns) from 2 database tables
Let's say I have class classA { void someMethod() { Thread a = new
Say have normal class in iOS, called A. I'd like to pass this as
Say I have a Listener class. I have class A and class M which

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.