Sha256: e7d3ec1474dd1d6c12bd0940c2b4abf76eda751f704aae34a7c6146920896228

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

Feature: implicitly defined subject

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

  While the examples below demonstrate how `subject` can be used as a
  user-facing concept, we recommend that you reserve it for support of custom
  matchers and/or extension libraries that hide its use from examples.

  Scenario: `subject` exposed in top level group
    Given a file named "top_level_subject_spec.rb" with:
      """ruby
      RSpec.describe Array do
        it "should be empty when first created" do
          expect(subject).to be_empty
        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
      RSpec.describe Array do
        describe "when first created" do
          it "should be empty" do
            expect(subject).to be_empty
          end
        end
      end
      """
    When I run `rspec nested_subject_spec.rb`
    Then the examples should all pass

  Scenario: `subject` in a nested group with a different class (innermost wins)
    Given a file named "nested_subject_spec.rb" with:
      """ruby
      class ArrayWithOneElement < Array
        def initialize(*)
          super
          unshift "first element"
        end
      end

      RSpec.describe Array do
        describe ArrayWithOneElement do
          context "referenced as subject" do
            it "contains one element" do
              expect(subject).to include("first element")
            end
          end
        end
      end
      """
    When I run `rspec nested_subject_spec.rb`
    Then the examples should all pass

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
opal-rspec-1.1.0.alpha3 rspec-core/upstream/features/subject/implicit_subject.feature
opal-rspec-1.1.0.alpha2 rspec-core/upstream/features/subject/implicit_subject.feature
opal-rspec-1.1.0.alpha1 rspec-core/upstream/features/subject/implicit_subject.feature
opal-rspec-1.0.0 rspec-core/upstream/features/subject/implicit_subject.feature
opal-rspec-1.0.0.alpha1 rspec-core/upstream/features/subject/implicit_subject.feature