forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmocha_extensions.rb
More file actions
66 lines (60 loc) · 1.87 KB
/
Copy pathmocha_extensions.rb
File metadata and controls
66 lines (60 loc) · 1.87 KB
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
# This chunk of monkey patching thanks to Simone Carletti,
# see https://github.com/floehopper/mocha/pull/19 for more information
module Mocha
class Expectation
def returns(*values, &block)
@return_values += if block_given?
ReturnValues.build(block)
else
ReturnValues.build(*values)
end
self
end
end
class SingleReturnValue
def evaluate
if @value.is_a?(Proc)
@value.call
else
@value
end
end
end
end
# allows setting up mocks/stubs that will be automatically applied any time
# this AR instance is instantiated, through find or whatever
# the record must be saved before calling any_instantiation, so that it has an id
module MochaAnyInstantiation
module ClassMethods
def reset_any_instantiation!
@@any_instantiation = {}
end
def add_any_instantiation(ar_obj)
raise(ArgumentError, "need to save first") if ar_obj.new_record?
@@any_instantiation[ [ar_obj.class.base_class, ar_obj.id] ] = ar_obj
# calling any_instantiation is likely to be because you're stubbing it,
# and to later be cached inadvertently from code that *thinks* it
# has a non-stubbed object. So let it dump, but not load (i.e.
# the MemoryStore and NilStore dumps that are just for testing,
# but just discard the result of dump)
def ar_obj.marshal_dump
nil
end
# no marshal_load; will raise an exception on load
ar_obj
end
def instantiate(*args)
if obj = @@any_instantiation[[base_class, args.first['id'].to_i]]
obj
else
super
end
end
end
def any_instantiation
ActiveRecord::Base.add_any_instantiation(self)
end
end
ActiveRecord::Base.singleton_class.prepend(MochaAnyInstantiation::ClassMethods)
ActiveRecord::Base.include(MochaAnyInstantiation)
ActiveRecord::Base.reset_any_instantiation!