File: null_object_doubles.feature

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (35 lines) | stat: -rw-r--r-- 1,409 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Feature: Null object doubles

  [Test doubles](./test-doubles) are strict by default, raising errors when they receive messages that have not
  been allowed or expected. You can chain `as_null_object` off of `double` in order to make
  the double "loose". For any message that has not explicitly allowed or expected, the double
  will return itself. It acts as a black hole null object, allowing arbitrarily deep method chains.

  Scenario: `as_null_object` allows arbitrarily deep message chains and returns itself
    Given a file named "as_null_object_spec.rb" with:
      """ruby
      RSpec.describe "as_null_object" do
        it "returns itself" do
          dbl = double("Some Collaborator").as_null_object
          expect(dbl.foo.bar.bazz).to be(dbl)
        end
      end
      """
     When I run `rspec as_null_object_spec.rb`
     Then the examples should all pass

  Scenario: Individual methods can still be allowed or expected
    Given a file named "as_null_object_spec.rb" with:
      """ruby
      RSpec.describe "as_null_object" do
        it "can allow individual methods" do
          dbl = double("Some Collaborator", :foo => 3).as_null_object
          allow(dbl).to receive(:bar).and_return(4)

          expect(dbl.foo).to eq(3)
          expect(dbl.bar).to eq(4)
        end
      end
      """
     When I run `rspec as_null_object_spec.rb`
     Then the examples should all pass