File: partial_test_doubles.feature

package info (click to toggle)
ruby-rspec 3.9.0c2e2m1s3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,612 kB
  • sloc: ruby: 67,456; sh: 1,572; makefile: 98
file content (70 lines) | stat: -rw-r--r-- 2,466 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
Feature: Partial test doubles

  A _partial test double_ is an extension of a real object in a system that is instrumented with
  test-double like behaviour in the context of a test. This technique is very common in Ruby
  because we often see class objects acting as global namespaces for methods. For example,
  in Rails:

  ```ruby
  person = double("person")
  allow(Person).to receive(:find) { person }
  ```

  In this case we're instrumenting Person to return the person object we've defined whenever
  it receives the `find` message. We can also set a message expectation so that the example
  fails if `find` is not called:

  ```ruby
  person = double("person")
  expect(Person).to receive(:find) { person }
  ```

  RSpec replaces the method we're stubbing or mocking with its own test-double like method.
  At the end of the example, RSpec verifies any message expectations, and then restores the
  original methods.

  Note: we recommend enabling the [`verify_partial_doubles`](../verifying-doubles/partial-doubles) config option.

  Scenario: Only the specified methods are redefined
    Given a file named "partial_double_spec.rb" with:
      """ruby
      RSpec.describe "A partial double" do
        # Note: stubbing a string like this is a terrible idea.
        #       This is just for demonstration purposes.
        let(:string) { "a string" }
        before { allow(string).to receive(:length).and_return(500) }

        it "redefines the specified methods" do
          expect(string.length).to eq(500)
        end

        it "does not effect other methods" do
          expect(string.reverse).to eq("gnirts a")
        end
      end
      """
     When I run `rspec partial_double_spec.rb`
     Then the examples should all pass

  Scenario: The original method is restored when the example completes
    Given a file named "partial_double_spec.rb" with:
      """ruby
      class User
        def self.find(id)
          :original_return_value
        end
      end

      RSpec.describe "A partial double" do
        it "redefines a method" do
          allow(User).to receive(:find).and_return(:redefined)
          expect(User.find(3)).to eq(:redefined)
        end

        it "restores the redefined method after the example completes" do
          expect(User.find(3)).to eq(:original_return_value)
        end
      end
      """
     When I run `rspec partial_double_spec.rb --order defined`
     Then the examples should all pass