Testing with RSpec

RSpec is Ruby's most popular behavior-driven testing framework, letting you describe what your code should do in clear, readable specs and prove it with expectations and matchers.

Learn Testing with RSpec in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll structure specs with describe/context/it, assert with matchers, share setup using let and hooks, and isolate code with mocks and stubs.

What You'll Learn in This Lesson

1️⃣ describe, context, it, and Matchers

describe names the thing under test, context groups examples by situation, and it holds one example. Inside, expect(actual).to matcher states what should be true.

RSpec ships with a rich set of matchers. let and subject keep specs concise.

2️⃣ Hooks, Mocks, and Stubs

before / after hooks run shared setup and teardown. A double stands in for a real object; allow stubs a return value, while expect(...).to receive mocks a required call.

Your turn. Complete the expectation so the greeter spec passes, then run it.

Write a small Stack class and cover it with two examples using let and matchers. Run with rspec stack_spec.rb .

📋 Quick Reference — RSpec

Practice quiz

What is RSpec?

  • a behavior-driven testing framework for Ruby
  • a database adapter
  • a deployment tool
  • a Ruby web framework

Answer: a behavior-driven testing framework for Ruby. RSpec is a popular behavior-driven development (BDD) testing framework for Ruby.

Which RSpec block declares an individual example (test case)?

  • context
  • subject
  • it
  • describe

Answer: it. An it block holds a single example, the smallest unit of an RSpec test.

What does describe usually name?

  • a gem dependency
  • the thing under test, such as a class or method
  • a single assertion
  • a test database

Answer: the thing under test, such as a class or method. describe groups examples for a particular class, method, or feature being tested.

What is the main purpose of a context block?

  • to define matchers
  • to run setup code only
  • to install gems
  • to group examples that share a particular situation or state

Answer: to group examples that share a particular situation or state. context groups examples under a specific condition, like when logged in, improving readability.

Which expectation checks value equality with eq?

  • expect(x).to eq(5)
  • x.should_equal 5
  • expect(x).is(5)
  • assert_equal(5, x)

Answer: expect(x).to eq(5). RSpec uses expect(x).to eq(5); eq tests value equality using ==.

What does let provide in RSpec?

  • a globally shared variable
  • a lazily evaluated, memoized helper available in examples
  • a way to skip tests
  • a database migration

Answer: a lazily evaluated, memoized helper available in examples. let defines a helper that is computed lazily on first use and memoized within an example.

When does a before(:each) hook run?

  • once for the whole file
  • only after all examples
  • never, it is deprecated
  • before each example in the group

Answer: before each example in the group. before(:each) runs its setup code before every example in the group.

What does double create in RSpec?

  • a duplicate test file
  • a second expectation
  • a test double (mock object) standing in for a real one
  • a real object from the database

Answer: a test double (mock object) standing in for a real one. double creates a lightweight test double you can stub or set message expectations on.

Which sets up a stub so a method returns a canned value?

  • double.return
  • allow(obj).to receive(:name).and_return("Ada")
  • expect(obj).to be_name
  • obj.stub_all

Answer: allow(obj).to receive(:name).and_return("Ada"). allow(obj).to receive(:name).and_return(value) stubs the method to return that value.

In a Rails app, what do request specs primarily test?

  • HTTP behavior end to end through routes and controllers
  • CSS styling
  • database backups
  • gem versions

Answer: HTTP behavior end to end through routes and controllers. Request specs exercise the full HTTP stack, from route to controller to response.