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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
module RSpec::Mocks
RSpec.describe Space do
let(:space) { Space.new }
let(:dbl_1) { Object.new }
let(:dbl_2) { Object.new }
describe "#verify_all" do
it "verifies all mocks within" do
verifies = []
allow(space.proxy_for(dbl_1)).to receive(:verify) { verifies << :dbl_1 }
allow(space.proxy_for(dbl_2)).to receive(:verify) { verifies << :dbl_2 }
space.verify_all
expect(verifies).to match_array([:dbl_1, :dbl_2])
end
def define_singleton_method_on_recorder_for(klass, name, &block)
recorder = space.any_instance_recorder_for(klass)
(class << recorder; self; end).send(:define_method, name, &block)
end
it 'verifies all any_instance recorders within' do
klass_1, klass_2 = Class.new, Class.new
verifies = []
# We can't `stub` a method on the recorder because it defines its own `stub`...
define_singleton_method_on_recorder_for(klass_1, :verify) { verifies << :klass_1 }
define_singleton_method_on_recorder_for(klass_2, :verify) { verifies << :klass_2 }
space.verify_all
expect(verifies).to match_array([:klass_1, :klass_2])
end
it 'does not reset the proxies (as that should be delayed until reset_all)' do
proxy = space.proxy_for(dbl_1)
reset = false
(class << proxy; self; end).__send__(:define_method, :reset) { reset = true }
space.verify_all
expect(reset).to eq(false)
end
end
describe "#reset_all" do
it "resets all mocks within" do
resets = []
allow(space.proxy_for(dbl_1)).to receive(:reset) { resets << :dbl_1 }
allow(space.proxy_for(dbl_2)).to receive(:reset) { resets << :dbl_2 }
space.reset_all
expect(resets).to match_array([:dbl_1, :dbl_2])
end
it "allows Array#reverse to be stubbed" do
# This is a regression check solved in rspec/rspec-mocks#1533 previously
# this was not possible without a change to the implementation
expect_any_instance_of(Array).to_not receive(:reverse)
end
end
describe "#proxies_of(klass)" do
it 'returns proxies' do
space.proxy_for("")
expect(space.proxies_of(String).map(&:class)).to eq([PartialDoubleProxy])
end
def create_generations
grandparent_class = Class.new
parent_class = Class.new(grandparent_class)
child_class = Class.new(parent_class)
grandparent = grandparent_class.new
parent = parent_class.new
child = child_class.new
return grandparent, parent, child
end
it 'returns only the proxies whose object is an instance of the given class' do
grandparent, parent, child = create_generations
space.proxy_for(grandparent)
parent_proxy = space.proxy_for(parent)
child_proxy = space.proxy_for(child)
expect(space.proxies_of(parent.class)).to contain_exactly(parent_proxy, child_proxy)
end
it 'looks in the parent space for matching proxies' do
_, parent, child = create_generations
parent_proxy = space.proxy_for(parent)
subspace = space.new_scope
child_proxy = subspace.proxy_for(child)
expect(subspace.proxies_of(parent.class)).to contain_exactly(parent_proxy, child_proxy)
end
end
it 'tracks proxies in parent and child space separately' do
proxy1 = space.proxy_for(Object.new)
subspace = space.new_scope
proxy2 = subspace.proxy_for(Object.new)
expect(space.proxies.values).to include(proxy1)
expect(space.proxies.values).not_to include(proxy2)
expect(subspace.proxies.values).to include(proxy2)
expect(subspace.proxies.values).not_to include(proxy1)
end
it "only adds an instance once" do
m1 = double("mock1")
expect {
space.ensure_registered(m1)
}.to change { space.proxies }
expect {
space.ensure_registered(m1)
}.not_to change { space.proxies }
end
[:ensure_registered, :proxy_for].each do |method|
describe "##{method}" do
define_method :get_proxy do |the_space, object|
the_space.__send__(method, object)
end
it 'returns the proxy for the given object' do
obj1 = Object.new
obj2 = Object.new
expect(get_proxy(space, obj1)).to equal(get_proxy(space, obj1))
expect(get_proxy(space, obj2)).to equal(get_proxy(space, obj2))
expect(get_proxy(space, obj1)).not_to equal(get_proxy(space, obj2))
end
it 'can still return a proxy from a parent context' do
proxy = get_proxy(space, Object)
subspace = space.new_scope
expect(get_proxy(subspace, Object)).to equal(proxy)
end
it "does not store a parent's proxy in the child space" do
get_proxy(space, Object)
subspace = space.new_scope
expect {
get_proxy(subspace, Object)
}.not_to change { subspace.proxies }.from({})
end
end
end
describe "#registered?" do
it 'returns true if registered in this space' do
space.ensure_registered(Object)
expect(space).to be_registered(Object)
end
it 'returns true if registered in a parent space' do
space.ensure_registered(Object)
expect(space.new_scope).to be_registered(Object)
end
it 'returns false if not registered in this or a parent space' do
expect(space.new_scope).not_to be_registered(Object)
end
end
describe "#constant_mutator_for" do
it 'returns the mutator for the given const name' do
the_space = RSpec::Mocks.space
stub_const("Foo", 3)
stub_const("Bar", 4)
expect(the_space.constant_mutator_for("Foo")).to equal(the_space.constant_mutator_for("Foo"))
expect(the_space.constant_mutator_for("Bar")).to equal(the_space.constant_mutator_for("Bar"))
expect(the_space.constant_mutator_for("Foo")).not_to equal(the_space.constant_mutator_for("Bar"))
end
it 'can still return a mutator from a parent context' do
the_space = RSpec::Mocks.space
stub_const("Foo", 3)
mutator = the_space.constant_mutator_for("Foo")
in_new_space_scope do
subspace = RSpec::Mocks.space
expect(subspace.constant_mutator_for("Foo")).to equal(mutator)
end
end
end
describe "#any_instance_recorder_for" do
it 'returns the recorder for the given class' do
expect(space.any_instance_recorder_for(String)).to equal(space.any_instance_recorder_for(String))
expect(space.any_instance_recorder_for(Symbol)).to equal(space.any_instance_recorder_for(Symbol))
expect(space.any_instance_recorder_for(String)).not_to equal(space.any_instance_recorder_for(Symbol))
end
it 'can still return a recorder from a parent context' do
recorder = space.any_instance_recorder_for(String)
subspace = space.new_scope
expect(subspace.any_instance_recorder_for(String)).to equal(recorder)
end
it "does not store a parent's proxy in the child space" do
space.any_instance_recorder_for(String)
subspace = space.new_scope
expect {
subspace.any_instance_recorder_for(String)
}.not_to change { subspace.any_instance_recorders }.from({})
end
end
it 'can be diffed in a failure when it has references to an error generator via a proxy' do
space1 = Space.new
space2 = Space.new
space1.proxy_for("")
space2.proxy_for("")
expect {
expect(space1).to eq(space2)
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, /Diff/)
end
it 'raises ArgumentError with message if object is symbol' do
space1 = Space.new
object = :subject
expected_message = "Cannot proxy frozen objects. Symbols such as #{object} cannot be mocked or stubbed."
expect { space1.proxy_for(object) }.to raise_error(ArgumentError, expected_message)
end
def in_new_space_scope
RSpec::Mocks.setup
yield
ensure
RSpec::Mocks.teardown
end
end
end
|