Tools, Best practices, and Teaching TDD

A short explanation of tools, libraries, best practices, and teaching TDD

The two test libraries generally used in the Rails community are Minitest and RSpec.

Minitest

Minitest is part of the Ruby Standard Library and is available everywhere we might use Ruby (1.9 and up).

It has a straightforward syntax that is the Ruby translation of the original SUnit and JUnit libraries (for Smalltalk and Java, respectively), and it is the default alternative for a new Rails project. Minitest is used by the Rails core team to test Rails and has Rails-specific extensions.

Minitest has a straightforward syntax

RSpec

RSpec is a separate testing tool designed to support an emphasis on specifying behavior rather than implementation, which is sometimes called behavior-driven development (BDD). Rather than using keywords like test and assert, RSpec uses expect and describe. BDD emphasizes setting expectations about the code not yet written rather than assertions about code in the past.

RSpec Syntax

RSpec has a quirky syntax that sometimes depends on metaprogrammingMetaprogramming is a programming technique in which computer programs have the ability to treat other programs as their data.. People often have strong feelings about it, both positive and negative. It’s more flexible, which means it’s more expressive, but it’s also more complicated and has a larger ecosystem of related tools.

Author’s Note: I believe that more Rails applications use RSpec than Minitest, though this is disputed.

Rspec has a quirky syntax

RSpec vs Minitest

The primary testing tool used in this course is RSpec because its expressiveness makes it easier to work with, even if it has a slightly steeper learning curve. There’s also a whole chapter on Minitest, and we’ll discuss most of the extra tools in a way that references both RSpec and Minitest. Most RSpec code examples have a corresponding Minitest version.