Search code examples
testingbackbone.jsjasminejasmine-jquery

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.


Solution

  • 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.