Contents

This page comes from RSpec Core 2.8.0. It is not part of the 3.12.1 docs. See the latest RSpec Core docs.

implicit subject

If the first argument to the outermost example group is a class, an instance of that class is exposed to each example via the subject() method.

Scenario: subject in top level group

  • Given a file named "top_level_subject_spec.rb" with:
    ruby
    describe Array, "when first created" do
      it "should be empty" do
        subject.should eq([])
      end
    end
  • When I run `rspec ./top_level_subject_spec.rb`
  • Then the examples should all pass

Scenario: subject in a nested group

  • Given a file named "nested_subject_spec.rb" with:
    ruby
    describe Array do
      describe "when first created" do
        it "should be empty" do
          subject.should eq([])
        end
      end
    end
  • When I run `rspec nested_subject_spec.rb`
  • Then the examples should all pass

Source: features/subject/implicit_subject.feature at v2.8.0, RSpec Core (MIT license).