rspec Tutorial

RSpec is a BDD tool used to specify and test Ruby programs. It is used primarily to specify and test classes and methods, i.e. for unit testing.

The rspec gem is just a meta-gem which brings in the three parts of RSpec. Those three parts are also a way to structure this documentation.

  • rspec-core provides RSpec’s way of structuring and running tests: the rspec command-line executable, the describe, context and it methods, shared examples, etc. It is documented in the RSpec Core topic.
  • rspec-expectations provides RSpec’s support for expecting test results: the expect/to expectation syntax and RSpec’s built-in matchers. (It also provides the deprecated should expectation syntax.) It is documented in the RSpec Expectations topic.
  • rspec-mocks provides RSpec’s support for test doubles: double, allow, expect, receive, have_received, etc. It is documented in the RSpec Mocks topic.

There is also the rspec-rails gem, which extends RSpec with support for testing the types of classes used in Rails applications, and with support for writing feature specs (acceptance tests) which test the application from the user’s point of view.

Official documentation for RSpec and rspec-rails is here: