Debian Codegraph @

rspec-core/features/subject/implicit_subject.feature

raw
1 Feature: Implicitly defined subject
2
3 If the first argument to an example group is a class, an instance of that
4 class is exposed to each example in that example group via the `subject`
5 method.
6
7 While the examples below demonstrate how `subject` can be used as a
8 user-facing concept, we recommend that you reserve it for support of custom
9 matchers and/or extension libraries that hide its use from examples.
10
11 Scenario: `subject` exposed in top level group
12 Given a file named "top_level_subject_spec.rb" with:
13 """ruby
14 RSpec.describe Array do
15 it "should be empty when first created" do
16 expect(subject).to be_empty
17 end
18 end
19 """
20 When I run `rspec ./top_level_subject_spec.rb`
21 Then the examples should all pass
22
23 Scenario: `subject` in a nested group
24 Given a file named "nested_subject_spec.rb" with:
25 """ruby
26 RSpec.describe Array do
27 describe "when first created" do
28 it "should be empty" do
29 expect(subject).to be_empty
30 end
31 end
32 end
33 """
34 When I run `rspec nested_subject_spec.rb`
35 Then the examples should all pass
36
37 Scenario: `subject` in a nested group with a different class (innermost wins)
38 Given a file named "nested_subject_spec.rb" with:
39 """ruby
40 class ArrayWithOneElement < Array
41 def initialize(*)
42 super
43 unshift "first element"
44 end
45 end
46
47 RSpec.describe Array do
48 describe ArrayWithOneElement do
49 context "referenced as subject" do
50 it "contains one element" do
51 expect(subject).to include("first element")
52 end
53 end
54 end
55 end
56 """
57 When I run `rspec nested_subject_spec.rb`
58 Then the examples should all pass