File: construction_spec.rb

package info (click to toggle)
ruby-rspec 3.5.0c3e0m0s0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,312 kB
  • ctags: 4,788
  • sloc: ruby: 62,572; sh: 785; makefile: 100
file content (130 lines) | stat: -rw-r--r-- 4,500 bytes parent folder | download | duplicates (3)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require 'support/doubled_classes'

module RSpec
  module Mocks
    RSpec.describe 'Constructing a verifying double' do
      include_context 'with isolated configuration'

      class ClassThatDynamicallyDefinesMethods
        def self.define_attribute_methods!
          define_method(:some_method_defined_dynamically) { true }
        end
      end

      module CustomModule
      end

      describe 'instance doubles' do
        it 'cannot be constructed with a non-module object' do
          expect {
            instance_double(Object.new)
          }.to raise_error(/Module or String expected/)
        end

        it 'can be constructed with a struct' do
          o = instance_double(Struct.new(:defined_method), :defined_method => 1)
          expect(o.defined_method).to eq(1)
        end

        it 'allows named constants to be looked up and declared to verifying double callbacks' do
          expect { |probe|
            RSpec.configuration.mock_with(:rspec) do |config|
              config.verify_doubled_constant_names = true
              config.when_declaring_verifying_double(&probe)
            end

            instance_double("RSpec::Mocks::ClassThatDynamicallyDefinesMethods")
          }.to yield_with_args(have_attributes :target => ClassThatDynamicallyDefinesMethods)
        end

        it 'allows anonymous constants to be looked up and declared to verifying double callbacks' do
          anonymous_module = Module.new
          expect { |probe|
            RSpec.configuration.mock_with(:rspec) do |config|
              config.verify_doubled_constant_names = true
              config.when_declaring_verifying_double(&probe)
            end

            instance_double(anonymous_module)
          }.to yield_with_args(have_attributes :target => anonymous_module)
        end

        it 'allows classes to be customised' do
          test_class = Class.new(ClassThatDynamicallyDefinesMethods)

          RSpec.configuration.mock_with(:rspec) do |config|
            config.when_declaring_verifying_double do |reference|
              reference.target.define_attribute_methods!
            end
          end

          instance_double(test_class, :some_method_defined_dynamically => true)
        end
      end

      describe 'class doubles' do
        it 'cannot be constructed with a non-module object' do
          expect {
            class_double(Object.new)
          }.to raise_error(/Module or String expected/)
        end

        it 'declares named modules to verifying double callbacks' do
          expect { |probe|
            RSpec.configuration.mock_with(:rspec) do |config|
              config.when_declaring_verifying_double(&probe)
            end
            class_double CustomModule
          }.to yield_with_args(have_attributes :target => CustomModule)
        end

        it 'declares anonymous modules to verifying double callbacks' do
          anonymous_module = Module.new
          expect { |probe|
            RSpec.configuration.mock_with(:rspec) do |config|
              config.when_declaring_verifying_double(&probe)
            end
            class_double anonymous_module
          }.to yield_with_args(have_attributes :target => anonymous_module)
        end
      end

      describe 'object doubles' do
        it 'declares the class to verifying double callbacks' do
          object = Object.new

          expect { |probe|
            RSpec.configuration.mock_with(:rspec) do |config|
              config.when_declaring_verifying_double(&probe)
            end
            object_double object
          }.to yield_with_args(have_attributes :target => object)
        end
      end

      describe 'when verify_doubled_constant_names config option is set' do

        before do
          RSpec::Mocks.configuration.verify_doubled_constant_names = true
        end

        it 'prevents creation of instance doubles for unloaded constants' do
          expect {
            instance_double('LoadedClas')
          }.to raise_error(VerifyingDoubleNotDefinedError)
        end

        it 'prevents creation of class doubles for unloaded constants' do
          expect {
            class_double('LoadedClas')
          }.to raise_error(VerifyingDoubleNotDefinedError)
        end
      end

      it 'can only be named with a string or a module' do
        expect { instance_double(1) }.to raise_error(ArgumentError)
        expect { instance_double(nil) }.to raise_error(ArgumentError)
      end
    end
  end
end