When I heard that Rails 3.1 was going to include CoffeeScript I decided to work to figure out how I could write both my production code as well as my specs in Coffeescript.
Using this gist as a guide, I came up with these detailed instructions:
guard init to create a Guardfile and edit it to contain the following:
Start up the Jasmine server using
rake jasmine and point your browser to http://localhost:8888. You should see 0 specs, 0 failures. Since you have changed the location that Jasmine looks for spec files, you aren’t picking up the example Jasmine specs any longer.
describe 'Math:', -> describe 'fib()', -> it 'should calculate the numbers correctly up to fib(16)', -> fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] expect(Math.fib(i)).toEqual fib[i] for i in [0..16]
Refresh your browser, you should see one failing spec. Guard has compiled your .coffee file into a .js file that Jasmine will use.
Math.fib = (n) -> s = 0 return s if n == 0 if n == 1 s += 1 else Math.fib(n - 1) + Math.fib(n - 2)
Refresh your Jasmine browser window and you should see 1 passing spec. Again, Guard has compiled your implementation file and Jasmine uses it to satisfy the spec.
At some point, it would be nice to figure out a way to run Jasmine specs directly from the .coffee files and against the implementation .coffee files without having to use Guard to compile them. With the above steps, you still have to check in the compiled files into source control so your tests can be run in the CI environment. If you miss one of the compiled specs or one of the compiled implementation files, your CI environment may report improper results.
If anyone has any ideas, I’d love to hear them.
About the Author
BiographyMore Content by Mike Gehard