This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 358
/
Copy pathmocks.rb
132 lines (119 loc) · 4.36 KB
/
mocks.rb
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
require 'rspec/support'
RSpec::Support.require_rspec_support 'caller_filter'
RSpec::Support.require_rspec_support 'warnings'
RSpec::Support.require_rspec_support 'ruby_features'
RSpec::Support.define_optimized_require_for_rspec(:mocks) { |f| require_relative f }
%w[
instance_method_stasher
method_double
argument_matchers
example_methods
proxy
test_double
argument_list_matcher
message_expectation
order_group
error_generator
space
mutate_const
targets
syntax
configuration
verifying_double
version
].each { |name| RSpec::Support.require_rspec_mocks name }
# Share the top-level RSpec namespace, because we are a core supported
# extension.
module RSpec
# Contains top-level utility methods. While this contains a few
# public methods, these are not generally meant to be called from
# a test or example. They exist primarily for integration with
# test frameworks (such as rspec-core).
module Mocks
# Performs per-test/example setup. This should be called before
# an test or example begins.
def self.setup
@space_stack << (@space = space.new_scope)
end
# Verifies any message expectations that were set during the
# test or example. This should be called at the end of an example.
def self.verify
space.verify_all
end
# Cleans up all test double state (including any methods that were
# redefined on partial doubles). This _must_ be called after
# each example, even if an error was raised during the example.
def self.teardown
space.reset_all
@space_stack.pop
@space = @space_stack.last || @root_space
end
# Adds an allowance (stub) on `subject`
#
# @param subject the subject to which the message will be added
# @param message a symbol, representing the message that will be
# added.
# @param opts a hash of options, :expected_from is used to set the
# original call site
# @yield an optional implementation for the allowance
#
# @example Defines the implementation of `foo` on `bar`, using the passed block
# x = 0
# RSpec::Mocks.allow_message(bar, :foo) { x += 1 }
def self.allow_message(subject, message, opts={}, &block)
orig_caller = opts.fetch(:expected_from) {
CallerFilter.first_non_rspec_line
}
space.proxy_for(subject).add_stub(orig_caller, message, opts, &block)
end
# Sets a message expectation on `subject`.
# @param subject the subject on which the message will be expected
# @param message a symbol, representing the message that will be
# expected.
# @param opts a hash of options, :expected_from is used to set the
# original call site
# @yield an optional implementation for the expectation
#
# @example Expect the message `foo` to receive `bar`, then call it
# RSpec::Mocks.expect_message(bar, :foo)
# bar.foo
def self.expect_message(subject, message, opts={}, &block)
orig_caller = opts.fetch(:expected_from) {
CallerFilter.first_non_rspec_line
}
space.proxy_for(subject).add_message_expectation(orig_caller, message, opts, &block)
end
# Call the passed block and verify mocks after it has executed. This allows
# mock usage in arbitrary places, such as a `before(:all)` hook.
def self.with_temporary_scope
setup
begin
yield
verify
ensure
teardown
end
end
class << self
# @private
attr_reader :space
end
@space_stack = []
@root_space = @space = RSpec::Mocks::RootSpace.new
# @private
IGNORED_BACKTRACE_LINE = 'this backtrace line is ignored'
# To speed up boot time a bit, delay loading optional or rarely
# used features until their first use.
autoload :AnyInstance, "rspec/mocks/any_instance"
autoload :ExpectChain, "rspec/mocks/message_chain"
autoload :StubChain, "rspec/mocks/message_chain"
autoload :MarshalExtension, "rspec/mocks/marshal_extension"
# Namespace for mock-related matchers.
module Matchers
autoload :HaveReceived, "rspec/mocks/matchers/have_received"
autoload :Receive, "rspec/mocks/matchers/receive"
autoload :ReceiveMessageChain, "rspec/mocks/matchers/receive_message_chain"
autoload :ReceiveMessages, "rspec/mocks/matchers/receive_messages"
end
end
end