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/report.rb,
lib/r_spec/clone/expectation_helper.rb,
lib/r_spec/clone/expectation_target.rb,
lib/r_spec/clone/expectation_helper/it.rb,
lib/r_spec/clone/expectation_helper/matcher.rb

Overview

The namespace of RSpec, which the clone takes for its words: describe, context, it and pending, at the top level of a spec file.

Examples:

require "r_spec"

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

  describe "#empty?" do
    it "is true when the array holds nothing" do
      expect([].empty?).to be_true
    end
  end
end

# spec/array_spec.rb:6: Success: expected 3 to eq 3
# spec/array_spec.rb:12: Success: expected true to be true

Defined Under Namespace

Modules: Clone

Class Method Summary collapse

Class Method Details

.context(description) { ... } ⇒ nil

Defines an example group that establishes a context, like empty array versus array with elements, and runs it in a child process.

Examples:

require "r_spec"

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

  it { is_expected.to raise_exception ZeroDivisionError }
end

# spec/zero_spec.rb:6: Success: expected #<ZeroDivisionError: divided by 0> to raise exception ZeroDivisionError

Parameters:

  • description (String) —

    a description, usually beginning with "when", "with" or "without"

Yields:

  • the specs of the context

Returns:

  • (nil)

Raises:

  • (ArgumentError) —

    without a block

  • (NotImplementedError) —

    when this Ruby cannot fork

  • (SystemExit) —

    when a spec of the context did not succeed



51
52
53
# File 'lib/r_spec.rb', line 51

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

.describe(const) { ... } ⇒ nil

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

# spec/string_spec.rb:4: Success: expected String to be String

Parameters:

  • const (Module, String) —

    the module under test, or a description

Yields:

  • the specs of the group

Returns:

  • (nil)

Raises:

  • (ArgumentError) —

    without a block



70
71
72
# File 'lib/r_spec.rb', line 70

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

.it(name = nil) { ... } ⇒ nil

Defines a concrete test case.

Examples:

require "r_spec"

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

# spec/next_spec.rb:3: Success: expected 42 to be 42

Parameters:

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

    the name of the example

Yields:

  • the example, with its expectations

Returns:

  • (nil)

Raises:

  • (ArgumentError) —

    without a block

  • (SystemExit) —

    when an expectation is not met



88
89
90
# File 'lib/r_spec.rb', line 88

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

.pending(message) { ... } ⇒ nil

Defines a pending test case: its block, if any, is never run.

Examples:

require "r_spec"

RSpec.pending "is not yet implemented"

# spec/later_spec.rb:3: Pending: is not yet implemented

Parameters:

  • message (String) —

    why the example is pending

Yields:

  • never: the block is not run

Returns:

  • (nil)


104
105
106
# File 'lib/r_spec.rb', line 104

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