RSpec in one lesson

Installing RSpec

RSpec is a gem with a few supporting gems; the install is one command and the runner is rspec:

gem install rspec

6 gems installed

A spec file

A spec file lives in spec/ and ends in _spec.rb. describe names the unit, it names a behaviour and expect(...).to is the assertion:

require_relative "../lib/calculator" RSpec.describe Calculator do it "adds" do expect(Calculator.new.add(2, 2)).to eq(4) end it "raises on zero" do expect { Calculator.new.divide(1, 0) } .to raise_error(ArgumentError, "division by zero") end end

eq is equality, raise_error takes a block because the call must not run before the matcher. The matcher list is longer than Minitest's: include, match, be_between, change, output.

Running it

rspec

.. Finished in 0.00633 seconds (files took 0.17427 seconds to load) 2 examples, 0 failures

rspec without an argument reads spec/. A single file is rspec spec/calc_spec.rb, and a single example is the file plus a line number: rspec spec/calc_spec.rb:10.

The same test in Minitest

The two tests, side by side. The behaviour is identical; the words differ:

RSpecMinitest spec style
expect(x).to eq(4)_(x).must_equal 4
expect { f }.to raise_error(E)_(-> { f }).must_raise E
described_classCalculator or let
before { }before { } in a describe block

RSpec's output names each example as it fails, which is the feature its users miss in Minitest. Minitest is in the standard toolchain of every Ruby and starts in milliseconds, which is the feature its users miss in RSpec. Both are fine; one per project is the rule.

What to watch for

expect without .to asserts nothing and passes. The line reads as an assertion and is a no-op, which is the quietest way to lose a test.

RSpec loads spec/spec_helper.rb only when the file requires it. Configuration put there and never required has no effect, and the run looks fine.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? class Calculator def add(a, b) = a + b end puts Calculator.new.add(2, 2)

    Answer

    4

    The method under test adds its two arguments, which is the value the first example expects.

  2. Where does a spec file live in an RSpec project?

    Answer

    In spec/ with a _spec.rb name

    The rspec command reads spec/ by default, and the file name ends in _spec.rb.

  3. What does expect(x).to eq(4) assert?

    Answer

    That x is 4 by equality

    eq is the equality matcher, and .to is what turns the expectation into an assertion.

  4. Why does raise_error take a block?

    Answer

    The call must not run before the matcher is ready to catch the error

    A plain expression would raise before the matcher saw it, which is the same reason Minitest uses a lambda.

  5. What does expect(x) without .to do?

    Answer

    Asserts nothing and passes

    The expectation alone is a no-op, which the page calls the quietest way to lose a test.

  6. What does described_class return inside RSpec.describe Calculator do ... end?

    Answer

    The Calculator class

    The describe argument becomes the described class, which saves repeating the constant in every example.

  7. What does rspec spec/calc_spec.rb:10 run?

    Answer

    The example at line 10 of that file

    A file plus a line number selects one example, which is the fastest way to rerun a failure.

  8. What does a bare rspec command read?

    Answer

    The spec/ folder

    The default path is spec/, which is why the convention matters.

  9. When does spec/spec_helper.rb load?

    Answer

    Only when a spec file requires it

    Configuration put there and never required has no effect, and the run still looks fine.

  10. Which sentence compares the two frameworks fairly?

    Answer

    RSpec documents a failure with the example description, while Minitest is bundled with Ruby and starts fast

    The page keeps both: each has a feature its users miss in the other, and one per project is the rule.

Article author: Arthur Isaev