DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. JavaScript
  4. Testing CoffeeScript in Rails

Testing CoffeeScript in Rails

Chris Smith user avatar by
Chris Smith
·
Mar. 11, 12 · Interview
Like (0)
Save
Tweet
Share
2.83K Views

Join the DZone community and get the full member experience.

Join For Free

As I mentioned in an earlier post on testing CoffeeScript in Node.js, Mocha is a flexible and feature-rich test framework for JavaScript. It is elegant, powerful, and a joy to use. But how do you use Mocha with Rails? Faced with this obstacle, I created MochaRails.

MochaRails is a mountable Rails engine that serves a browser-based Mocha test suite, along with your development JavaScript files, via the Asset Pipeline. It loads Chai for both should- and expect-style assertions, although since Mocha is decoupled from the assertion library, you can use another assertion library if you choose (or even write your own.)

Using should-style assertions and CoffeeScript, Mocha tests are the best-looking implementation of BDD that I have ever seen in any language. For example, here are some simple tests for a Backbone.js View.

describe 'ItemView', ->

  before ->
    @itemView = new Backbone.View
      tagName: 'li'
      className: 'item'

  describe "el", ->

    it 'should return a value', ->
      expect(@itemView.el).to.exist

    it "should return an 'li' element", ->
      @itemView.el.tagName.should.equal "LI"

  describe '$el', ->

    it "should have class 'item'", ->
      @itemView.$el.hasClass('item').should.be.true
I would argue that even if you can't or don't want to write your production code in CoffeeScript, you should write your tests in CoffeeScript. By eliminating so much visual noise, CoffeeScripts syntax helps bring the goal of BDD--specs that anyone can read to understand the intended behavior of your system--so much closer to reality.

Once you've installed MochaRails and added the test above (simple to do, and explained in the project's Readme), open localhost:3000/mocha to execute your test suite. If you've never seen Mocha's clean and elegant report for browser tests, prepare to be impressed.
Now that's clean! It may seem almost too clean with just three tests, but once you have scores of contexts and hundreds of tests, you'll appreciate the aesthetic. Errors are colored red of course, but Mocha errors do not light up the page like a garish billboard.


Mocha also supports asynchronous code with a simple callback mechanism. I highly recommend you browse Mocha's features and give MochaRails a try.

 

Coffeescript

Published at DZone with permission of Chris Smith, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • NoSQL vs SQL: What, Where, and How
  • How To Set Up and Run Cypress Test Cases in CI/CD TeamCity
  • File Uploads for the Web (2): Upload Files With JavaScript
  • Integrate AWS Secrets Manager in Spring Boot Application

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: