1

I'm trying to get Jasmine (jasmine-headless-webkit), coffeescript and backbone to work together.

I'm close - I've got my site running on coffeescript and backbone, and I can run coffeescript written tests - but my problem is, I can't get my coffeescript tests to do anything interesting.

If I try create an instance of a model in a test, I'll get an error:

ReferenceError: Can't find variable: xxxx

FYI - I'm using this with guard-jasmine-headless-webkit...

I've been working with the Jasmine.yml file - but still can't get these tests to work. Does anyone have any advice?

Daniel D
  • 3,637
  • 5
  • 36
  • 41
  • possible duplicate of [Classes defined in CoffeeScript not found by Jasmine specs](http://stackoverflow.com/questions/8310329/classes-defined-in-coffeescript-not-found-by-jasmine-specs) – Trevor Burnham Dec 20 '11 at 19:10
  • 1
    The cause is either 1) the file wrapper (you need to make `xxx` a global by writing `window.xxx = xxx`), or 2) the order in which your scripts are being loaded (you need to ensure that `xxx` is assigned *before* your tests run). – Trevor Burnham Dec 20 '11 at 19:11
  • Can I still use window.xxx if I'm using jasmine-headless-webkit? – Daniel D Dec 21 '11 at 01:33
  • 1
    Not sure. To support all target environments, use `(window ? global).xxx`. – Trevor Burnham Dec 21 '11 at 01:42
  • Yes, you can use window.xxx since JHW uses a WebKit browser to run the tests. – John Bintz Dec 22 '11 at 14:41

2 Answers2

2

Trevor was right - the issue was the order in which scripts were loaded (in my jasmine.yml)

I wound up cleaning it out for the most part...

helpers:
     - helpers/**/*.js

src_dir:
  - app/assets/javascripts
  - vendor/assets/javascripts

src_files:
  - "**/*.*"

spec_dir: spec/javascripts

spec_files:
  - "**/*[Ss]pec.*"

stylesheets:
  - stylesheets/**/*.css
Daniel D
  • 3,637
  • 5
  • 36
  • 41
  • Daniel, how did you end up configuring your jasmine.yml file? I can't seem to get our CI to load jquery before underscore/backbone, nor application.js first. – Fudgie Mar 06 '12 at 23:38
  • Also, the src_dir should better be full path to app/assets/javascripts , I changed to app/assets than sprocket can't find the script files in require. – Jimchao Apr 03 '12 at 23:21
0

Friendly update: if you're using Rails asset pipeline for coffeescript compilation, a recent release of the Jasmine gem adds support for coffeescript via rails asset pipeline. huge improvement. its the 1.2.0 version (not the release candidates, the official 1.2.0 release) https://github.com/pivotal/jasmine-gem

GregT
  • 2,194
  • 2
  • 19
  • 15