Classes defined in CoffeeScript not found by Jasmine specs
-
25-10-2019 - |
سؤال
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 ...
المحلول
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.
نصائح أخرى
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