Classes defined in CoffeeScript not found by Jasmine specs

我是研究僧i 提交于 2020-01-01 14:36:06

问题


I am building a backbone.js app on a Rails 3.1 back-end. I'm using CoffeeScript to write the backbone classes, and Jasmine (via jasmine-headless-webkit) for testing.

Given the following (partial) tree:

.
├── app
│   ├── assets
│   │   ├── javascripts
│   │   │   └── views
│   │   │       ├── avia_view.js.coffee
├── spec
│   ├── javascripts
│   │   └── views
│   │       └── avia_view_spec.js.coffee

... I would expect avia_view_spec.js.coffee to know about Avia.AviaView, which is defined in avia_view.js.coffee.

However, I get the following output from running bundle exec jasmine-headless-webkit:

Running Jasmine specs...
F

Avia.AviaView render creates a new MatricesView . (/home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee:10)
  ReferenceError: Can't find variable: Avia in /home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee (line ~5)
  ReferenceError: Can't find variable: Avia in /home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee (line ~10)

My jasmine.yml contains the following:

src_files:
  - public/javascripts/prototype.js
  - public/javascripts/effects.js
  - public/javascripts/controls.js
  - public/javascripts/dragdrop.js
  - public/javascripts/application.js
  - public/javascripts/**/*.js

I think I need to tell Jasmine to load the contents of avia_view.js.coffee but I'm not entirely sure how. Adding an explicit reference in the src_files section in jasmine.yml doesn't seem to make a difference ...

Could someone please tell me what I'm doing wrong here? I suspect it's something simple ...


回答1:


Without having seen to much of your code, I'd suspect it beacuse of CoffeeScript's function wrapping (docs). You need to ensure that all the symbols you want to use are exported to somewhere you can get at them (here is a thorough discussion about that).

Edit: here's another longish and theoretical but good documentation on this topic.




回答2:


Try adding this to your avia_view.js.coffee

(exports ? this).Avia = Avia 

See this for a detailed explanation.

Alternatively try this;

window.Avia = Avia

We encountered the same problem; I highly recommend JasmineRice



来源:https://stackoverflow.com/questions/8310329/classes-defined-in-coffeescript-not-found-by-jasmine-specs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!