Tyler DeWitt
Tyler DeWitt

Reputation: 23576

Using gon with jasmine-rails

Our backbone app uses gon. When we try to run our tests, we are getting a gon is undefined error in the console of the browser. Our layout file includes a call to include_gon, but that file is not being loaded by jasmine, so jasmine is failing in our first javascript file that contains gon. We tried creating a helper to assign the gon variable to an empty hash (like a fixture), but the helper was called after the first call to gon and therefore didn't fix our issue.

Upvotes: 0

Views: 292

Answers (2)

james
james

Reputation: 4047

What worked for me was to define window.gon = {} inside the test like so:

describe("A suite is just a function", function() {
  beforeEach(function() {
    window.gon = {}
    gon.test = "this"
  })
  it ("should find gon", function() {
    expect(gon.test).toBe("this")
  })
})

Upvotes: 0

Tyler DeWitt
Tyler DeWitt

Reputation: 23576

The secret was to use the asset pipeline to define the load order of my files. I commented out these lines from jasmine.yml

# path to parent directory of src_files
# relative path from Rails.root
# defaults to app/assets/javascripts
#src_dir: "app/assets/javascripts"

# list of file expressions to include as source files
# relative path from src_dir
#src_files:
#  - "application.{js.coffee,js,coffee}"

And created spec.js.coffee with these lines:

#= require application
#= require jasmine-jquery

Now my js files get loaded in order and I am good to go.

Upvotes: 0

Related Questions