To add a collaborator to this project you will need to use the Relish gem to add the collaborator via a terminal command. Soon you'll be able to also add collaborators here!
More about adding a collaboratorexplicit subject
Use subject
in the group scope to explicitly define the value that is
returned by the subject
method in the example scope.
Note that 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.
- Scenarios
-
- `subject` in top level group
- `subject` in a nested group
- Access `subject` from `before` block
- Invoke helper method from `subject` block
- `subject` block is invoked at most once per example
- `subject!` bang method
- `subject` in top level group
-
- Given
-
a file named "top_level_subject_spec.rb" with:
RSpec.describe Array, "with some elements" do subject { [1,2,3] } it "should have the prescribed elements" do expect(subject).to eq([1,2,3]) end end
- When
-
I run
rspec top_level_subject_spec.rb
- Then
- the examples should all pass
- `subject` in a nested group
-
- Given
-
a file named "nested_subject_spec.rb" with:
RSpec.describe Array do subject { [1,2,3] } describe "with some elements" do it "should have the prescribed elements" do expect(subject).to eq([1,2,3]) end end end
- When
-
I run
rspec nested_subject_spec.rb
- Then
- the examples should all pass
- Access `subject` from `before` block
-
- Given
-
a file named "top_level_subject_spec.rb" with:
RSpec.describe Array, "with some elements" do subject { [] } before { subject.push(1,2,3) } it "should have the prescribed elements" do expect(subject).to eq([1,2,3]) end end
- When
-
I run
rspec top_level_subject_spec.rb
- Then
- the examples should all pass
- Invoke helper method from `subject` block
-
- Given
-
a file named "helper_subject_spec.rb" with:
RSpec.describe Array do def prepared_array; [1,2,3] end subject { prepared_array } describe "with some elements" do it "should have the prescribed elements" do expect(subject).to eq([1,2,3]) end end end
- When
-
I run
rspec helper_subject_spec.rb
- Then
- the examples should all pass
- `subject` block is invoked at most once per example
-
- Given
-
a file named "nil_subject_spec.rb" with:
RSpec.describe Array do describe "#[]" do context "with index out of bounds" do before { expect(Array).to receive(:one_two_three).once.and_return([1,2,3]) } subject { Array.one_two_three[42] } it { is_expected.to be_nil } end end end
- When
-
I run
rspec nil_subject_spec.rb
- Then
- the examples should all pass
- `subject!` bang method
-
- Given
-
a file named "subject_bang_spec.rb" with:
RSpec.describe Array do describe '#pop' do let(:prepared_array) { [1,2,3] } subject! { prepared_array.pop } it "removes the last value from the array" do expect(prepared_array).to eq([1,2]) end it "returns the last value of the array" do expect(subject).to eq(3) end end end
- When
-
I run
rspec subject_bang_spec.rb
- Then
- the examples should all pass
Last published almost 7 years ago by myronmarston.