Module: RSpec

Defined in:
lib/r_spec.rb,
lib/r_spec/clone.rb,
lib/r_spec/clone/dsl.rb,
lib/r_spec/clone/error.rb,
lib/r_spec/clone/logger.rb,
lib/r_spec/clone/expectation_helper.rb,
lib/r_spec/clone/expectation_target.rb,
lib/r_spec/clone/error/reserved_method.rb,
lib/r_spec/clone/expectation_helper/it.rb,
lib/r_spec/clone/expectation_helper/its.rb,
lib/r_spec/clone/error/undefined_subject.rb,
lib/r_spec/clone/expectation_target/base.rb,
lib/r_spec/clone/expectation_target/block.rb,
lib/r_spec/clone/expectation_target/value.rb,
lib/r_spec/clone/error/pending_expectation.rb,
lib/r_spec/clone/expectation_helper/shared.rb,
lib/r_spec/clone/error/undefined_described_class.rb

Overview

Top level namespace for the RSpec clone.

Examples:

The true from the false

require "r_spec"

RSpec.describe "The true from the false" do
  it { expect(false).not_to be true }
end

# Output to the console
#   Success: expected false not to be true.

The basic behavior of arrays

require "r_spec"

RSpec.describe Array do
  describe "#size" do
    it "correctly reports the number of elements in the Array" do
      expect([1, 2, 3].size).to eq 3
    end
  end

  describe "#empty?" do
    it "is empty when no elements are in the array" do
      expect([].empty?).to be_true
    end

    it "is not empty if there are elements in the array" do
      expect([1].empty?).to be_false
    end
  end
end

# Output to the console
#   Success: expected to eq 3.
#   Success: expected to be true.
#   Success: expected to be false.

An inherited definition of let

require "r_spec"

RSpec.describe Integer do
  let(:answer) { 42 }

  it "returns the value" do
    expect(answer).to be(42)
  end

  context "when the number is incremented" do
    let(:answer) { super().next }

    it "returns the next value" do
      expect(answer).to be(43)
    end
  end
end

# Output to the console
#   Success: expected to be 42.
#   Success: expected to be 43.

Defined Under Namespace

Modules: Clone

Class Method Summary collapse

Class Method Details

.context(description) ⇒ Object

Defines an example group that establishes a specific context, like _empty array_ versus _array with elements_.

Examples:

require "r_spec"

RSpec.context "when divided by zero" do
  subject { 42 / 0 }

  it { is_expected.to raise_exception ZeroDivisionError }
end

# Output to the console
#   Success: divided by 0.

Parameters:

  • description (String)

    A description that usually begins with “when”, “with” or “without”.

  • block (Proc)

    The block to define the specs.



86
87
88
# File 'lib/r_spec.rb', line 86

def self.context(description, &)
  Clone::Dsl.context(description, &)
end

.describe(const) ⇒ Object

Defines an example group that describes a unit to be tested.

Examples:

require "r_spec"

RSpec.describe String do
  it { expect(described_class).to be String }
end

# Output to the console
#   Success: expected to be String.
require "r_spec"

RSpec.describe String do
  let(:foo) { "foo" }

  describe "+" do
    it("concats") { expect(foo + "bar").to eq "foobar" }
  end
end

# Output to the console
#   Success: expected to eq "foobar".

Parameters:

  • const (Module, String)

    A module to include in block context.

  • block (Proc)

    The block to define the specs.



120
121
122
# File 'lib/r_spec.rb', line 120

def self.describe(const, &)
  Clone::Dsl.describe(const, &)
end

.it(name = nil) ⇒ nil

Defines a concrete test case.

The test is performed by the block supplied to &block.

It is usually used inside a RSpec::Clone::Dsl.describe or RSpec::Clone::Dsl.context section.

Examples:

The integer after 41

require "r_spec"

RSpec.it { expect(41.next).to be 42 }

# Output to the console
#   Success: expected to be 42.

Parameters:

  • name (String, nil) (defaults to: nil)

    The name of the spec.

  • block (Proc)

    An expectation to evaluate.

Returns:

  • (nil)

    Write a message to STDOUT.

Raises:

  • (SystemExit)

    Terminate execution immediately by calling ‘Kernel.exit(false)` with a failure message written to STDERR.



146
147
148
# File 'lib/r_spec.rb', line 146

def self.it(name = nil, &)
  Clone::Dsl.it(name, &)
end

.pending(message) ⇒ nil

Defines a pending test case.

‘&block` is never evaluated. It can be used to describe behaviour that is not yet implemented.

It is usually used inside a RSpec::Clone::Dsl.describe or RSpec::Clone::Dsl.context section.

Examples:

require "r_spec"

RSpec.pending "is implemented but waiting" do
  expect something to be finished
end

RSpec.pending "is not yet implemented and waiting"

# Output to the console
#   Warning: is implemented but waiting.
#   Warning: is not yet implemented and waiting.

Parameters:

  • message (String)

    The reason why the example is pending.

Returns:

  • (nil)

    Write a message to STDOUT.



176
177
178
# File 'lib/r_spec.rb', line 176

def self.pending(message)
  Clone::Dsl.pending(message)
end