Tools, Best practices, and Teaching TDD
A short explanation of tools, libraries, best practices, and teaching TDD
We'll cover the following
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.
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
Author’s Note: I believe that more Rails applications use RSpec than Minitest, though this is disputed.
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.