RSpec Testing Framework

Qualified supports the RSpec behavior-driven testing framework.

These notes are adopted from rspec-core.

Basic Example

RSpec uses the words "describe" and "it" so we can express concepts like a conversation:

"Describe an order."
"It sums the prices of its line items."
describe Order do
  it "sums the prices of its line items" do
    order =

    order.add_entry( =>
      :price =>, :USD)
    order.add_entry( =>
      :price =>, :USD),
      :quantity => 2

    expect( eq(, :USD))

The describe method creates an ExampleGroup. Within the block passed to describe you can declare examples using the it method.

Under the hood, an example group is a class in which the block passed to describe is evaluated. The blocks passed to it are evaluated in the context of an instance of that class.

Get Started

Start with a simple example of behavior you expect from your system:

describe Calculator do
  describe '#add' do
    it 'returns the sum of its arguments' do
      expect(, 2)).to eq(3)

Run this by clicking the VALIDATE TEST CASES in the challenge creator, or RUN TESTS when taking a challenge, and you will see a failure similar to:

uninitialized constant Calculator (NameError)

Address the failure by defining a skeleton of the Calculator class in the Solution code area:

class Calculator
  def add(a, b)

Now run the spec again, and watch the expectation fail:

expected: 3 got: nil (compared using ==)

Implement the simplest solution, by changing the definition of Calculator#add to:

def add(a, b)
  a + b

Now run the spec again, and watch it pass:

    returns the sum of its arguments
      Test Passed

Learn More

You can learn how to use it on the RSpec Website.