5

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.

Morten
  • 1,819
  • 5
  • 28
  • 37
  • does `Player` have the same problem? – Thilo Nov 03 '11 at 10:37
  • 1
    is this only a problem for Jasmine? how about the code for the site? Also, maybe this helps: http://stackoverflow.com/questions/6150455/structuring-coffeescript-code – Thilo Nov 03 '11 at 10:40

4 Answers4

8

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!

GregT
  • 2,194
  • 2
  • 19
  • 15
  • Thank you for following up. This is of course the best solution and is what I ended up doing. – Morten Feb 27 '12 at 11:21
4

I wasn't willing to accept modifying the namespace of my code by using an @ symbol in front of all my backbone classes, so I dug around some more and the solution that worked for me was to require the application file in my spec/javascripts/spec.js.coffee file

#= require application
rstawarz
  • 41
  • 1
  • 1
    While this can work it seems... heavy-handed for unit tests. Not all your tests should require *every single* js file that your application manifest is requiring. IMO you should only be requiring the file you need per-test. – nzifnab Sep 05 '13 at 02:09
1
window.game = () -> new MyGame

This will assign a function that returns a new MyGame to window.game. Did you not just want the new instance directly?

window.game = new MyGame

The window.game approach also seem awkward to me.

How about this

describe "MyGame", ->
   game = null

   beforeEach ->
     game = new MyGame

   it "should have two players", ->
      expect(game.opponents.length).toEqual 2
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • In that case I get a `ReferenceError: Can't find variable: MyGame`. Btw. I'm using Rails 3.1 and my `jasmine.yml` has `src_files: - "app/assets/**/*.js" - "app/assets/**/*.coffee"` and asset pipeline set up correctly. – Morten Nov 03 '11 at 07:48
  • 1
    How is MyGame made visible to the test file (or the outside world in general)? Unfortunately there is no real standard for that, and I cannot speak for Rails. I use CommonJS, and I would have `{MyGame} = require 'models/game'` on the top of the file. But I don't know if that applies to your setup. – Thilo Nov 03 '11 at 07:52
  • I have updated the question. I think it's more a reference problem as you describe and I guess it has something to do with the Rails 3.1 asset pipeline. – Morten Nov 03 '11 at 08:15
0

I have solved the problem by defining every class as class window.MyGame for example. In the spec files I put #= require my_file_name in the top.

Furthermore I have placed both jasminerice.js.coffee and jquery.js in app/assets/javascripts. This might not be the best solution as I assume they should be placed in spec/javascripts/helpers as my spec.js.coffee's content is #=require_tree ./.

I'm aware that this is not very elegant but it might help others in the same situation. @Thilo thanks for your inputs.

Morten
  • 1,819
  • 5
  • 28
  • 37