开发者

CoffeeScript unit testing?

I'm using CoffeeScript in a Rails application, and I would like to unit test it. Google didn't turn up anything, is there any way to do it short of writing my own testing framework or test开发者_如何学JAVAing the JavaScript that CoffeeScript outputs?


You can use any javascript testing framework with CoffeeScript. This will be testing the Javascript that CoffeeScript outputs which is necessary since CoffeeScript itself can't be executed.

Writing your own testing framework for CoffeeScript is fun (I did) but entirely uneccessary.

UPDATE: Jasmine tests can be run on node.js in which case both the tests and the code under test can be CoffeeScript, without the need for any compilation step.


You can use QUnit "as-is", but still only write coffee-script - and no glue-code.
I have a very small, pure coffee-script project on github as an example - rubyann.

The HTML test page rubyann_tests.html, references the rubyann_tests.coffee file which tests jquery.rubyann.coffee. I didn't write any javascript or any other code to make this work.

The tests only run on Chrome on your local machine if you use the command-line argument --allow-file-access-from-files. But it works on Firefox and even IE without issues.

addendum - the tests are also setup to run on the command line via Node/gulp/qunitjs - download the repo and type npm run test


I'm testing CoffeeScript in my Rails app with QUnit, and have written up how I'm doing it here: http://effectif.com/coffeescript/qunit-boilerplate

The most interesting thing in my write-up is the use of the callback to Coffee.load to guarantee that files containing tests get loaded after the files that contain the code under test:

<script type="text/coffeescript">
  for file in ['models', 'controllers']                                             
    lib = "../../app/assets/javascripts/#{file}.js.coffee"                          
    load_test = ->                                                                  
      test = "#{file}_test.coffee"                                                  
      -> CoffeeScript.load(test)                                                    
    CoffeeScript.load lib, load_test()  
</script>

The need for currying the test variable is explained in the article...


For Coffee-Script Unit testing you can try Beast-Test it was written from the ground up for coffee-script. FYI i am the own but i think you will like it none the less. It is similar to JUnit

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜