Udemy
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Testing Ruby with RSpec: The Complete Guide
Highest Rated
Rating: 4.8 out of 5(2,118 ratings)
22,088 students

Testing Ruby with RSpec: The Complete Guide

Master the art of test driven development (TDD) in Ruby using the RSpec Gem. No previous testing experience needed!
Created byBoris Paskhaver
Last updated 1/2026
English

What you'll learn

  • Master the syntax and structure of RSpec, the most popular Ruby Gem for testing
  • Utilize test-driven development principles to design and implement clean test specs in Ruby
  • Explore the wide collection of RSpec matchers available to test your code
  • Reduce dependencies in your test suite by mocking objects with class and instance doubles

Course content

5 sections61 lectures7h 32m total length
  • Welcome to RSpec13:03

    Welcome to the course! This lecture offers a quick introduction to the RSpec Gem as well as the benefits of testing. The various pieces of the library -- the core runner, expectations, and mocks -- are also introduced.

  • About Me0:56

    Get to know a little about your instructor.

  • Unit Tests vs End-to-End (E2E) Tests9:16

    Unit tests target a specific "unit" or piece of an application, such as a single class or method. They create isolation between coupled components to ensure each piece stands by itself. In comparison, end-to-end tests test an application or a large feature as a whole. Integration tests fall somewhere in the middle. In this lesson, we talk extensively about these types of tests and introduce the testing pyramid.

  • Setup Ruby on macOS Computers1:45

    This lecture offers a written tutorial for installing the Homebrew package manager, the rbenv Ruby version manager, and a modern version of Ruby on a macOS operating system. Windows users are advised to install Ruby through the RubyInstaller.org website.

  • Setup Ruby on Windows Computers0:39

    Learn how to install Ruby on a Windows computer.

  • Install RSpec3:11

    Ruby ships with a package manager called Gem. In this lesson, we utilize it to download the RSpec testing library.

  • Install Visual Studio Code for macOS0:56

    Visual Studio Code is a popular open-source text editor that is supported by Microsoft. This lesson covers the installation of the software on a macOS machine as well as the setup of the recommended Ruby extension for syntax highlighting. Windows users are advised to follow the instructions on the VSCode website to setup the app on their computers. If you prefer another text editor, that is totally fine as well.

  • Start a Project with rspec --init10:17

    The rspec --init command creates a base skeleton for an RSpec project. It creates a spec_helper.rb file where high-level, global RSpec settings for the project can be declared. There are some recommended settings that can be enabled by removing the =begin and =end lines in the file.

  • Download Course Files (Optional)0:28

    Download the complete course files here!

  • Test-Driven Development5:28

    Test-driven development (TDD) is a testing paradigm that argues that tests should be written first, before the code. This approach forces the developer to think critically about the implementation of the feature, one method at a time. This lesson describes the red-green-refactor pattern for practicing TDD. Red means a failing spec, green means a passing spec, and refactor means optimizing the code for clarity and efficiency.

  • The describe Method4:42

    The describe method on the top-level RSpec module creates an example group. An example group contains one or more examples. An example is the technical RSpec term for a test. In this lesson, we walk through the basic syntax and structure of setting up a sample example group (hey, that rhymes!)

  • The it Method5:33

    The it method creates an example (i.e. a focused test). The it method accepts a string -- ideally, it should be one that makes the example read like a sentence (i.e. it "should shuffle the deck" or it "can communicate with database"). After the string argument, pass the method a block. The block will contain the assertions for that example.

  • The expect and eq Methods8:01

    The expect method accepts what will be evaluated --it can be an object, a class, a method, or a plain Ruby expression like 1 + 1. The method returns an object that includes a to method, which is invoked with a matcher. A matcher is a type of assertion; there are various matchers in RSpec for equality, inequality, identity, inclusion and more. In this lesson, we create a sample Card object and write our first expectation for it.

  • The describe, it and expect Methods
  • Reading Failures7:29

    The rspec command in the Terminal can be followed with a path to a spec file's location. The command starts the RSpec test runner. The Terminal output includes a list of all examples that failed, including the line number that encountered an error or inconsistency. In this lesson, we read over the output and prepare to fix our failing specs.

  • Making the Specs Pass8:57

    It's time to make the Card class a reality! In this lesson, using our failing specs to guide us, we define a new Card class with a type attribute and a public reader method. It's important to not speed ahead and try to fix everything at once. Use each failure from the RSpec output to determine the next line of code to write. Fix one error, then run the rspec command again, and repeat.

  • QUIZ: TDD and RSpec Methods
  • Multiple Examples in Example Group8:01

    An example group can contain multiple examples. Some testing advocates argue that each example should only have one assertion. In this lesson, we expand the Card class to have a rank and a suit and write the corresponding specs.

  • Fixing Failing Specs Again8:23

    In this lesson, we continue the practice of using our failing specs to drive the development of the code. We update the Card class to have two initialize arguments, two reader methods, and two instance variables. We also discuss some of the drawbacks of our current examples, particularly the duplication.

  • Making Specs Pass
  • Reducing Duplication - Before Hooks and Instance Variables10:58

    Multiple examples in a test suite often rely on the same piece of data or a common object. Duplicating the code to create or access that object in each example creates a lot of duplication. To DRY up the code, this lesson introduces before hooks and instance variables. A hook is a piece of code that runs automatically at a specific time during testing. By default, the before hook executes a block of code before each example. Within the block, we assign values to instance variables to preserve values once the block execution ends. These instance variables are then available to be used within any examples defined below.

  • Reducing Duplication: Helper Methods4:12

    The block passed to the describe method provides a regular environment for Ruby expressions. We can define helper methods there to be invoked by the examples. Like a before block, this approach also creates separation between the examples.

  • Problems With Mutation6:03

    In this lesson, we discuss the limitations of a helper method approach. Subsequent calls to the same helper method in an example will return a new object each time. This makes it impossible to mutate a single object's state without using extra variables.

  • Reducing Duplication: The let Method15:05

    The let method defines a memoized, lazy-loaded variable that is available to all examples in the current context. The method accepts a symbol for the name of the variable and a block for the value that variable should be assigned. Lazy-loaded means the variable will not be declared until it is used in a specific example. Memoized means multiple references to the variable in the same example will not require a reevaluation of the block passed to the let method.

  • Custom Error Messages4:39

    To have RSpec display a custom error message when an expectation fails, pass a string as the second argument to the to method. In this lesson, we compare the different Terminal outputs for the default error message and our custom one.

  • Removing Duplication
  • The context Method and Nested Describes8:17

    describe method invocations can be nested inside other describe blocks. This is done to provide context about the specific circumstance or situation that an example is being run. context is an available alias for the describe method -- it can be used in the same fashion. In this lesson, we create nested describe blocks to test the even? predicate method in Ruby.

  • before and after Hooks9:07

    A hook allows the developer to run code during certain moments or events in the test suite's execution. The before and after run a block of code before or after a given criteria. When given a symbol of :example, the hook will run before / after each individual test. When given a symbol of :context, the hook will run once before / after all tests in the current block. In a real-life scenario, :context is used for high-level setup and teardown operations like connecting and disconnecting to a database (which is an expensive operation that you don't want to perform for each test).

  • Nested Logic: Hooks12:00

    Hooks gain an additional layer of complexity when describe / context blocks are nested within other describe / context blocks. The before(:context) hooks runs once before all tests in the current context (i.e. the current block). The before(:example) hook runs once before each test in the current context (which includes all nested blocks as well). If there is a before(:example) hook defined at multiple levels, each one will run in sequence, starting from the top-most block and proceeding downwards.

  • Single Context Hooks
  • Multi-Context Hooks
  • Nested Logic: Overwriting Let Variables8:53

    let variables can be set to different values in nested blocks. Ruby  / RSpec will search for the name in the current scope, then proceed upwards if it is unable to find it. Instead of using multiple variable names and adding confusion, it's much more elegant to reuse the let variable and assign it new, relevant values in each testing context.

Requirements

  • Intermediate knowledge of the Ruby programming language (classes, objects, blocks, etc)
  • Modern version of Ruby (>3.0)
  • Text editor (VSCode is recommended)

Description

Welcome to Testing Ruby with RSpec: The Complete Guide!

This course offers a comprehensive overview of the RSpec testing library for the Ruby programming language. RSpec is the most popular Ruby Gem of all time, with over 300 million downloads to date.

If you're new to the topic, testing is the practice of "writing code that confirms that other code works as expected". Tests control for regressions, which are changes to the code that break the program. Tests give you the confidence to refactor existing code because you know that you will honor the original purpose of the code.

The benefits of testing extend outside of your codebase. In this course, we'll cover test-driven development (TDD), a methodology in which we write our tests first, before the implementation. Test-driven development often leads to simpler, more decoupled code. Thus, testing can make you a better developer. Tests force you to think critically about the program and its features: the classes, the objects, the methods and more.

Testing Ruby with RSpec begins with the RSpec essentials and proceeds to more complex topics including:

  • Installation

  • Project Initialization

  • Test-Driven Development (TDD)

  • let variables

  • before and after Hooks

  • Subjects

  • Shared Examples

  • Shared Context

  • Built-in Matchers

  • Mocks and Doubles

  • Instance Doubles

  • Class Doubles

As a software engineer and consultant who's worked with Ruby for 5+ years, I'm excited to introduce you to the awesome RSpec library, its elegant syntax, and all of its fun quirks.

Thanks for checking out the course!

Who this course is for:

  • Intermediate Ruby developers interested in upgrading their skill set
  • Programmers who want to explore the fundamentals of testing and TDD