Testing Coffeescript with Jasmine and Rails 3.1

Say I have a class in Coffeescript:

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

who would like to experience in Jasmine:

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

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

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

The approach window.gamealso seems uncomfortable to me. If I try to define it as @game = new MyGame, I get an error ReferenceError: Can't find variable: MyGame, but I think this is due to how Coffeescript wraps things up?

UPDATE: The problem is more like the reference problem described above. I'm running with guard-jasminethat 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.ymlfile has:

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

I get a ReferenceError: Can't find variable: MyGame, so I consider this either something with a Rails 3.1 resource pipeline or the way Coffeescript wraps objects.

+5
4

coffeescript @ :

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

, , , :

describe "MyGame", ->
   beforeEach ->
     @game = new MyGame

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

, coffeescript , , . , - . @, , , , , . , , . , !

+8

, @ , -, , , , , spec/javascripts/spec.js.coffee file

#= require application
+4
window.game = () -> new MyGame

, MyGame window.game. ?

window.game = new MyGame

window.game .

describe "MyGame", ->
   game = null

   beforeEach ->
     game = new MyGame

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

, class window.MyGame, . #= require my_file_name .

, jasminerice.js.coffee, jquery.js app/assets/javascripts. , , spec/javascripts/helpers, spec.js.coffee #=require_tree ./.

I know that this is not very elegant, but can help others in the same situation. @Thilo thanks for your inputs.

0
source

All Articles