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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
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
describe 'any_instance' do
let(:test_class) { Class.new(ClassThatDynamicallyDefinesMethods) }
let(:not_implemented_error) { "#{test_class} does not implement #some_invalid_method" }
before(:each) do
RSpec.configuration.mock_with(:rspec) do |config|
config.verify_partial_doubles = true
config.when_declaring_verifying_double do |reference|
reference.target.define_attribute_methods! if reference.target == test_class
end
end
end
it 'calls the callback for expect_any_instance_of' do
expect_any_instance_of(test_class).to receive(:some_method_defined_dynamically)
expect {
expect_any_instance_of(test_class).to receive(:some_invalid_method)
}.to raise_error(RSpec::Mocks::MockExpectationError, not_implemented_error)
expect(test_class.new.some_method_defined_dynamically).to eq(nil)
end
it 'calls the callback for allow_any_instance_of' do
allow_any_instance_of(test_class).to receive(:some_method_defined_dynamically)
expect {
allow_any_instance_of(test_class).to receive(:some_invalid_method)
}.to raise_error(RSpec::Mocks::MockExpectationError, not_implemented_error)
expect(test_class.new.some_method_defined_dynamically).to eq(nil)
end
it 'should not call the callback if verify_partial_doubles is off' do
RSpec.configuration.mock_with(:rspec) do |config|
config.verify_partial_doubles = false
end
expect(test_class.method_defined?(:some_method_defined_dynamically)).to be_falsey
end
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
|