Search code examples
ruby-on-railsrubybackbone.jsruby-mochakonacha

Konacha, Mocha, BackboneJS and Ruby on Rails configuration


I am using Konacha to test a BackboneJS application in my Ruby on Rails application. I have read about every tutorial on the web and it shows how easy it is to set up and get working. Unfortunately, I am not having this level of success. Here is what I have:

app/assets/javascripts/app_specific/itapp/models/post.js

Itapp.Models.Post = Backbone.Model.extend({
  isFirstPost: function() {
    return (this.get('id') === Itapp.bootstrap.posts[0].get('id'));
  },
});

spec/javascripts/app_specific/itapp/models/post_spec.js

//= require spec_helper

var expect = chai.expect;

describe("Posts", function() {
  it("should have a first post", function() {
    //just trying anything to get some kind of valid error/issue
    //I could put anything here and it wouldn't matter, just FYI
    expect(typeof this.isFirstPost).to.equal('function');
  });
});

spec/javascripts/spec_helper.js file:

// Require the appropriate asset-pipeline files:
//= require application

//Any other testing specific code here...
//Custom matchers, etc....

Konacha.mochaOptions.ignoreLeaks = true

beforeEach(function() {
  return window.page = $("#konacha");
});

// set the Mocha test interface
// see http://mochajs.org/#interfaces
mocha.ui('bdd');

//
// ignore the following globals during leak detection
mocha.globals(['YUI']);

//
// or, ignore all leaks
mocha.ignoreLeaks();

//
// set slow test timeout in ms
mocha.timeout(5);

//
// Show stack trace on failing assertion.
chai.config.includeStack = true;

I do have a config/initializers/konacha.rb file:

Konacha.configure do |config|
  config.spec_dir     = "spec/javascripts"
  config.spec_matcher = /_spec\.|_test\./
  config.stylesheets  = %w(application)
  config.driver = :selenium
end if defined?(Konacha)

The error I am getting:

Error: Failed to load app_specific/itapp/collections/posts_spec.js. Perhaps it failed to compile? Check the rake output for errors.

Checking the rake output:

ActionView::Template::Error: couldn't find file 'application' which I am requiring in my spec_helper.js

So for some reason even though in my spec_helper I am trying to load the BackboneJS application for the testing environment it is not able to find it.

Any thoughts/ideas that I should try to get this communicating/working?

--Mike Riley


Solution

  • I figured this out. The problem was that it was not finding the file under app/assets/javascripts/ correctly. I needed to do this at the top of the spec/javascripts/app_specific/itapp/models/post_spec.js file:

    //= require app_specific/itapp/models/post
    

    Once I did that it was able to find the associated code that I am testing against. I will need to work a bit more to clean up the path in a spec_helper.js file, but I am no longer blocked on this.