Ruby Testing

Posted by Flatiron School  /  June 19, 2013

The following is a guest post by Sagar Shah and originally appeared on his blog. Sagar is currently a student at The Flatiron School. You can follow him on Twitter here.

This weekend I scoured for a presentation related to the topics that we’re beginning to cover in class. I came across the following slide deck from Emily Price titled “Testing Ruby using RSPEC with Factory Girl and Autotest”.

The first two weeks of class we’ve really focused on just getting our programs to work and getting familiar with Ruby. However, late last week we began covering the concept of test-driven development. Emily Price’s slide deck gives a high level overview of the topic.

Testing best practices

The presentation’s author emphasized the following best practices for testing:

  • write tests first: testing is a continuous process of making your code fail, fixing it so it passes, refactoring, and repeating.

  • test your edge cases: there are always scenarios in which your code will fail that you may not think of right off the bat. Get creative and try to create tests for those scenarios that might break your code or give you strange results.

  • test behavior, not implementation: this essentially means that you want to test if the code produces the desired result, not necessarily how it produces it produces the result.

What the heck is RSPEC, Factory Girl, and Autotest anyway?

RSPEC is a testing tool for Ruby, and as with all things Ruby it was designed to “make Test-Driven Development a productive and enjoyable experience.” Factory Girl and Autotest are both gems that work with RSPEC to aid with testing. For example, Autotest is a gem that automatically runs your tests each time you change or save a file. It allows for continuous, quick testing so that you rarely have to open up the browser to see what’s happening to your code.

OK, so I’m not an expert in test-driven development yet. But, as they say, “the beginning of wisdom is to call things by their right names.” Now I know what RSPEC, Factory Girl, and Autotest are and have some best practices to refer to. Thanks Emily for your speakerdeck presentation!