Daniel Irvine on building software
CoffeeScript in Rails, the TDD way
20 July 2014
Here’s a quick summary of CoffeeScript development with Rails.
For Vim usage, you’ll need vim-coffee-script for its syntax highlighting at least. It also registers the CoffeeScript file type.
Teaspoon for running CoffeeScript specs
Vim plus Tmux is a great way to run tests without having to leave Vim. Teaspoon is easy to setup in this environment. After installing Teaspoon, I created the file
~/.vim/ftplugin/coffee.vim with the following line:
map <buffer> <leader>r :w <CR> :call VimuxRunCommand(clear; rake teaspoon)
This allows me to run all of my tests within the Rails solution. (Soon I’ll figure out how to run just one test and add it in here, too.)
The Teaspoon Quick Start Walkthrough is a great guide for writing your first CoffeeScript tests.
The asset pipeline
I didn’t have this set up in my Rails project, so I’ve had to add in the various pieces manually. To get both the asset pipeline and Teaspoon running, all of the following dependencies must be added to the
gem 'sprockets-rails' gem 'coffee-rails' gem 'uglifier' gem 'teaspoon' gem 'turbolinks'
Then the asset pipeline must be enabled for each Rails environment. So for example, in
config/environments/development.rb I added the following two lines.
config.assets.debug = true config.assets.enabled = true
That should be enough to get started.