假设我在 Coffeescript 有一堂课:
class MyGame
constructor: () ->
@me = new Player
@opponents = [new Player, new Player]
想在 Jasmine 中测试:
describe "MyGame", ->
beforeEach ->
window.game = new MyGame
it "should have two players", ->
expect(window.game.opponents.length).toEqual 2
但我得到错误TypeError: Result of expression 'window.game.opponents' [undefined] is not an object.
?
这种window.game
方法对我来说似乎也很尴尬。@game = new MyGame
如果我在收到错误时尝试定义它,ReferenceError: Can't find variable: MyGame
但我想这与 Coffeescript 的包装方式有关?
更新:这个问题看起来更像是一个参考问题,如上所述。我正在跑步guard-jasmine
,看起来像
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
我的jasmine.yml
文件有:
src_files:
- "app/assets/**/*.js"
- "app/assets/**/*.coffee"
spec_files:
- '**/*[sS]pec.js.coffee'
asset_pipeline_paths:
- app/assets
- spec/javascripts
我得到了一个,ReferenceError: Can't find variable: MyGame
所以我认为它要么是 Rails 3.1 资产管道的东西,要么是 Coffeescript 包装对象的方式。