meleyal / backbone-on-rails

A simple gem for using Backbone.js with Rails.
MIT License
809 stars 125 forks source link

Add option to generate Mocha/Jasmine tests #19

Closed mulderp closed 12 years ago

mulderp commented 12 years ago

As I am going through the Peepcode tutorials (which discuss backbone in the context of Sinatra/JS) by using backbone-on-rails, I am discovering that support for tests or specs is missing. That could be useful for playing with models in the web-browser.

To get this running, one might propose to create a /test-backbone directory with a SpecRunner.html and the Jasmine-lib. @topfunky uses a reference in the SpecRunner.html:

<script type="text/javascript" src="lib/jasmine-1.0.2/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-1.0.2/jasmine-html.js"></script>

Additionally, the collections, models and specs should be loaded, e.g. such as these:

<script src="../app/assets/javascript/Tunes.js"></script>
<!-- include spec files here... -->
<script type="text/javascript" src="spec/TunesSpec.js"></script>

Not a big issue, but eventually interesting to note.

mulderp commented 12 years ago

sorry, just see this is a duplication of https://github.com/meleyal/backbone-on-rails/issues/5

mulderp commented 12 years ago

I get some first results by using Jasminerice as @quartzmo proposed in principle, it looks easy to add jasminerice and go to http://0.0.0.0:3000/jasmine for seeing the spec. Still looking in some issues with getting all dependencies loaded

in spec/javascripts/spec.js.coffee, I try

spec/javascripts/spec.js.coffee //= require jquery //= require jquery_ujs //= require underscore //= require backbone //= require Tunes //= require modernizr-1.6.min

//= require peepcode2

=require_tree ./

Hmm.. personally, I prefer this to a strategy that seems to be discussed often in blogs, namely the use of Guard to automatically compile files, evt. Guard is needed too in this setup (?) still examining

mulderp commented 12 years ago

ok, regarding dependencies, the above spec.js.coffee works, (with Tunes your general application javascript)

In the spec, you need to declare the full namespace of the model-under-test, like so:

describe "Album", -> beforeEach -> @album = new Tunes.Models.Album(albumData[0])

meleyal commented 12 years ago

I'm working on the same thing at the moment, also with jasminerice.

A neat trick is to just include your application manifest in each spec, therefore ensuring that all dependencies are met:

#= require application

describe 'Album', ->
...
meleyal commented 12 years ago

I've updated the title of this issue to be more specific to Jasmine, as I think that could be a useful feature.

meleyal commented 12 years ago

Marking this as closed as it would be better implemented as a standalone gem, maybe one that hooks into the generators of this one.