File: expiration_spec.rb

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 (90 lines) | stat: -rw-r--r-- 2,542 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
module RSpec
  module Mocks
    RSpec.describe "After a test double has been torn down" do
      RSpec.shared_examples_for "expiration" do
        before do
          expect(dbl).to receive(:foo).at_least(:once)
          allow(dbl).to receive(:bar)
          dbl.foo

          RSpec::Mocks.verify
          RSpec::Mocks.teardown
          RSpec::Mocks.setup
        end

        it 'disallows previously mocked methods' do
          expect { dbl.foo }.to raise_error(ExpiredTestDoubleError)
        end

        it 'disallows previously stubbed methods' do
          expect { dbl.bar }.to raise_error(ExpiredTestDoubleError)
        end

        it 'disallows stubbing new methods (with receive)' do
          expect {
            allow(dbl).to receive(:bazz)
          }.to raise_error(ExpiredTestDoubleError)
        end

        it 'disallows stubbing new methods (with receive_messages)' do
          expect {
            allow(dbl).to receive_messages(:bazz => 3)
          }.to raise_error(ExpiredTestDoubleError)
        end

        it 'disallows stubbing new message chains' do
          expect {
            allow(dbl).to receive_message_chain(:bazz, :bam, :goo)
          }.to raise_error(ExpiredTestDoubleError)
        end

        it 'disallows mocking new methods' do
          expect {
            expect(dbl).to receive(:bazz)
          }.to raise_error(ExpiredTestDoubleError)
        end

        it 'disallows being turned into a null object' do
          expect { dbl.as_null_object }.to raise_error(ExpiredTestDoubleError)
        end

        it 'disallows being checked for nullness' do
          expect { dbl.null_object? }.to raise_error(ExpiredTestDoubleError)
        end

      end

      context "for a plain double" do
        let(:dbl) { double }
        include_examples "expiration"
      end

      class ExpiredInstanceInterface
        def foo;  end
        def bar;  end
        def bazz; end
      end

      class ExpiredClassInterface
        def self.foo;  end
        def self.bar;  end
        def self.bazz; end
      end

      context "for an instance_double" do
        let(:dbl) { instance_double(ExpiredInstanceInterface) }
        include_examples "expiration"
      end

      context "for a class_double" do
        let(:dbl) { class_double(ExpiredClassInterface) }
        include_examples "expiration"
      end

      context "for an object_double" do
        let(:dbl) { object_double(ExpiredInstanceInterface.new) }
        include_examples "expiration"
      end
    end
  end
end