opal-rspec 0.4.0.beta3 → 0.4.0.beta4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/.gitmodules +15 -0
- data/.travis.yml +12 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +3 -1
- data/Gemfile +6 -7
- data/README.md +2 -0
- data/Rakefile +12 -50
- data/lib/opal/rspec/version.rb +1 -1
- data/lib/opal/rspec.rb +14 -0
- data/opal/opal/rspec/async.rb +146 -11
- data/opal/opal/rspec/fixes.rb +18 -8
- data/opal/opal/rspec/requires.rb +45 -0
- data/opal/opal/rspec.rb +1 -24
- data/opal-rspec.gemspec +1 -1
- data/spec/async_spec.rb +4 -5
- data/spec/matchers_spec.rb +20 -0
- data/spec/named_subject_spec.rb +11 -0
- data/spec/should_syntax_spec.rb +17 -0
- data/vendor_lib/rspec/autorun.rb +2 -0
- data/vendor_lib/rspec/core/backport_random.rb +302 -0
- data/vendor_lib/rspec/core/backtrace_formatter.rb +65 -0
- data/vendor_lib/rspec/core/command_line.rb +36 -0
- data/vendor_lib/rspec/core/configuration.rb +1129 -0
- data/vendor_lib/rspec/core/configuration_options.rb +143 -0
- data/vendor_lib/rspec/core/drb_command_line.rb +26 -0
- data/vendor_lib/rspec/core/drb_options.rb +87 -0
- data/vendor_lib/rspec/core/dsl.rb +26 -0
- data/vendor_lib/rspec/core/example.rb +312 -0
- data/vendor_lib/rspec/core/example_group.rb +540 -0
- data/vendor_lib/rspec/core/filter_manager.rb +224 -0
- data/vendor_lib/rspec/core/flat_map.rb +17 -0
- data/vendor_lib/rspec/core/formatters/base_formatter.rb +291 -0
- data/vendor_lib/rspec/core/formatters/base_text_formatter.rb +307 -0
- data/vendor_lib/rspec/core/formatters/deprecation_formatter.rb +193 -0
- data/vendor_lib/rspec/core/formatters/documentation_formatter.rb +67 -0
- data/vendor_lib/rspec/core/formatters/helpers.rb +82 -0
- data/vendor_lib/rspec/core/formatters/html_formatter.rb +155 -0
- data/vendor_lib/rspec/core/formatters/html_printer.rb +408 -0
- data/vendor_lib/rspec/core/formatters/json_formatter.rb +99 -0
- data/vendor_lib/rspec/core/formatters/progress_formatter.rb +32 -0
- data/vendor_lib/rspec/core/formatters/snippet_extractor.rb +101 -0
- data/vendor_lib/rspec/core/formatters.rb +54 -0
- data/vendor_lib/rspec/core/hooks.rb +535 -0
- data/vendor_lib/rspec/core/memoized_helpers.rb +431 -0
- data/vendor_lib/rspec/core/metadata.rb +313 -0
- data/vendor_lib/rspec/core/mocking/with_absolutely_nothing.rb +11 -0
- data/vendor_lib/rspec/core/mocking/with_flexmock.rb +27 -0
- data/vendor_lib/rspec/core/mocking/with_mocha.rb +52 -0
- data/vendor_lib/rspec/core/mocking/with_rr.rb +27 -0
- data/vendor_lib/rspec/core/mocking/with_rspec.rb +27 -0
- data/vendor_lib/rspec/core/option_parser.rb +234 -0
- data/vendor_lib/rspec/core/ordering.rb +154 -0
- data/vendor_lib/rspec/core/pending.rb +110 -0
- data/vendor_lib/rspec/core/project_initializer.rb +88 -0
- data/vendor_lib/rspec/core/rake_task.rb +128 -0
- data/vendor_lib/rspec/core/reporter.rb +132 -0
- data/vendor_lib/rspec/core/ruby_project.rb +44 -0
- data/vendor_lib/rspec/core/runner.rb +97 -0
- data/vendor_lib/rspec/core/shared_context.rb +53 -0
- data/vendor_lib/rspec/core/shared_example_group/collection.rb +27 -0
- data/vendor_lib/rspec/core/shared_example_group.rb +146 -0
- data/vendor_lib/rspec/core/version.rb +7 -0
- data/vendor_lib/rspec/core/warnings.rb +22 -0
- data/vendor_lib/rspec/core/world.rb +131 -0
- data/vendor_lib/rspec/core.rb +203 -0
- data/vendor_lib/rspec/expectations/differ.rb +154 -0
- data/vendor_lib/rspec/expectations/errors.rb +9 -0
- data/vendor_lib/rspec/expectations/expectation_target.rb +87 -0
- data/vendor_lib/rspec/expectations/extensions/object.rb +29 -0
- data/vendor_lib/rspec/expectations/extensions.rb +1 -0
- data/vendor_lib/rspec/expectations/fail_with.rb +79 -0
- data/vendor_lib/rspec/expectations/handler.rb +68 -0
- data/vendor_lib/rspec/expectations/syntax.rb +182 -0
- data/vendor_lib/rspec/expectations/version.rb +8 -0
- data/vendor_lib/rspec/expectations.rb +75 -0
- data/vendor_lib/rspec/matchers/built_in/base_matcher.rb +68 -0
- data/vendor_lib/rspec/matchers/built_in/be.rb +213 -0
- data/vendor_lib/rspec/matchers/built_in/be_instance_of.rb +15 -0
- data/vendor_lib/rspec/matchers/built_in/be_kind_of.rb +11 -0
- data/vendor_lib/rspec/matchers/built_in/be_within.rb +55 -0
- data/vendor_lib/rspec/matchers/built_in/change.rb +141 -0
- data/vendor_lib/rspec/matchers/built_in/cover.rb +21 -0
- data/vendor_lib/rspec/matchers/built_in/eq.rb +22 -0
- data/vendor_lib/rspec/matchers/built_in/eql.rb +23 -0
- data/vendor_lib/rspec/matchers/built_in/equal.rb +48 -0
- data/vendor_lib/rspec/matchers/built_in/exist.rb +26 -0
- data/vendor_lib/rspec/matchers/built_in/has.rb +48 -0
- data/vendor_lib/rspec/matchers/built_in/include.rb +61 -0
- data/vendor_lib/rspec/matchers/built_in/match.rb +17 -0
- data/vendor_lib/rspec/matchers/built_in/match_array.rb +51 -0
- data/vendor_lib/rspec/matchers/built_in/raise_error.rb +154 -0
- data/vendor_lib/rspec/matchers/built_in/respond_to.rb +74 -0
- data/vendor_lib/rspec/matchers/built_in/satisfy.rb +30 -0
- data/vendor_lib/rspec/matchers/built_in/start_and_end_with.rb +48 -0
- data/vendor_lib/rspec/matchers/built_in/throw_symbol.rb +94 -0
- data/vendor_lib/rspec/matchers/built_in/yield.rb +297 -0
- data/vendor_lib/rspec/matchers/built_in.rb +39 -0
- data/vendor_lib/rspec/matchers/compatibility.rb +14 -0
- data/vendor_lib/rspec/matchers/configuration.rb +113 -0
- data/vendor_lib/rspec/matchers/dsl.rb +23 -0
- data/vendor_lib/rspec/matchers/generated_descriptions.rb +35 -0
- data/vendor_lib/rspec/matchers/matcher.rb +301 -0
- data/vendor_lib/rspec/matchers/method_missing.rb +12 -0
- data/vendor_lib/rspec/matchers/operator_matcher.rb +99 -0
- data/vendor_lib/rspec/matchers/pretty.rb +70 -0
- data/vendor_lib/rspec/matchers/test_unit_integration.rb +11 -0
- data/vendor_lib/rspec/matchers.rb +633 -0
- data/vendor_lib/rspec/mocks/any_instance/chain.rb +92 -0
- data/vendor_lib/rspec/mocks/any_instance/expectation_chain.rb +47 -0
- data/vendor_lib/rspec/mocks/any_instance/message_chains.rb +75 -0
- data/vendor_lib/rspec/mocks/any_instance/recorder.rb +200 -0
- data/vendor_lib/rspec/mocks/any_instance/stub_chain.rb +45 -0
- data/vendor_lib/rspec/mocks/any_instance/stub_chain_chain.rb +23 -0
- data/vendor_lib/rspec/mocks/argument_list_matcher.rb +104 -0
- data/vendor_lib/rspec/mocks/argument_matchers.rb +264 -0
- data/vendor_lib/rspec/mocks/arity_calculator.rb +66 -0
- data/vendor_lib/rspec/mocks/configuration.rb +111 -0
- data/vendor_lib/rspec/mocks/error_generator.rb +203 -0
- data/vendor_lib/rspec/mocks/errors.rb +12 -0
- data/vendor_lib/rspec/mocks/example_methods.rb +201 -0
- data/vendor_lib/rspec/mocks/extensions/marshal.rb +17 -0
- data/vendor_lib/rspec/mocks/framework.rb +36 -0
- data/vendor_lib/rspec/mocks/instance_method_stasher.rb +112 -0
- data/vendor_lib/rspec/mocks/matchers/have_received.rb +99 -0
- data/vendor_lib/rspec/mocks/matchers/receive.rb +112 -0
- data/vendor_lib/rspec/mocks/matchers/receive_messages.rb +72 -0
- data/vendor_lib/rspec/mocks/message_expectation.rb +643 -0
- data/vendor_lib/rspec/mocks/method_double.rb +209 -0
- data/vendor_lib/rspec/mocks/method_reference.rb +95 -0
- data/vendor_lib/rspec/mocks/mock.rb +7 -0
- data/vendor_lib/rspec/mocks/mutate_const.rb +406 -0
- data/vendor_lib/rspec/mocks/object_reference.rb +90 -0
- data/vendor_lib/rspec/mocks/order_group.rb +82 -0
- data/vendor_lib/rspec/mocks/proxy.rb +269 -0
- data/vendor_lib/rspec/mocks/proxy_for_nil.rb +37 -0
- data/vendor_lib/rspec/mocks/space.rb +95 -0
- data/vendor_lib/rspec/mocks/standalone.rb +3 -0
- data/vendor_lib/rspec/mocks/stub_chain.rb +51 -0
- data/vendor_lib/rspec/mocks/syntax.rb +374 -0
- data/vendor_lib/rspec/mocks/targets.rb +90 -0
- data/vendor_lib/rspec/mocks/test_double.rb +109 -0
- data/vendor_lib/rspec/mocks/verifying_double.rb +77 -0
- data/vendor_lib/rspec/mocks/verifying_message_expecation.rb +60 -0
- data/vendor_lib/rspec/mocks/verifying_proxy.rb +151 -0
- data/vendor_lib/rspec/mocks/version.rb +7 -0
- data/vendor_lib/rspec/mocks.rb +100 -0
- data/vendor_lib/rspec/support/caller_filter.rb +56 -0
- data/vendor_lib/rspec/support/spec/deprecation_helpers.rb +29 -0
- data/vendor_lib/rspec/support/spec/in_sub_process.rb +40 -0
- data/vendor_lib/rspec/support/spec/stderr_splitter.rb +50 -0
- data/vendor_lib/rspec/support/spec.rb +14 -0
- data/vendor_lib/rspec/support/version.rb +7 -0
- data/vendor_lib/rspec/support/warnings.rb +41 -0
- data/vendor_lib/rspec/support.rb +6 -0
- data/vendor_lib/rspec/version.rb +5 -0
- data/vendor_lib/rspec-expectations.rb +1 -0
- data/vendor_lib/rspec.rb +3 -0
- metadata +163 -4
- data/opal/opal/rspec/rspec.js +0 -20384
@@ -0,0 +1,374 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Mocks
|
3
|
+
# @api private
|
4
|
+
# Provides methods for enabling and disabling the available syntaxes
|
5
|
+
# provided by rspec-mocks.
|
6
|
+
module Syntax
|
7
|
+
# @api private
|
8
|
+
def self.warn_about_should!
|
9
|
+
@warn_about_should = true
|
10
|
+
end
|
11
|
+
|
12
|
+
# @api private
|
13
|
+
def self.warn_unless_should_configured(method_name)
|
14
|
+
if @warn_about_should
|
15
|
+
RSpec.deprecate(
|
16
|
+
"Using `#{method_name}` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax",
|
17
|
+
:replacement => "the new `:expect` syntax or explicitly enable `:should`"
|
18
|
+
)
|
19
|
+
|
20
|
+
@warn_about_should = false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# @api private
|
25
|
+
# Enables the should syntax (`dbl.stub`, `dbl.should_receive`, etc).
|
26
|
+
def self.enable_should(syntax_host = default_should_syntax_host)
|
27
|
+
@warn_about_should = false
|
28
|
+
return if should_enabled?(syntax_host)
|
29
|
+
|
30
|
+
syntax_host.class_exec do
|
31
|
+
def should_receive(message, opts={}, &block)
|
32
|
+
::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
|
33
|
+
opts[:expected_from] ||= CallerFilter.first_non_rspec_line
|
34
|
+
::RSpec::Mocks.expect_message(self, message, opts, &block)
|
35
|
+
end
|
36
|
+
|
37
|
+
def should_not_receive(message, &block)
|
38
|
+
::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
|
39
|
+
opts = {:expected_from => CallerFilter.first_non_rspec_line}
|
40
|
+
::RSpec::Mocks.expect_message(self, message, opts, &block).never
|
41
|
+
end
|
42
|
+
|
43
|
+
def stub(message_or_hash, opts={}, &block)
|
44
|
+
::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
|
45
|
+
if ::Hash === message_or_hash
|
46
|
+
message_or_hash.each {|message, value| stub(message).and_return value }
|
47
|
+
else
|
48
|
+
opts[:expected_from] = CallerFilter.first_non_rspec_line
|
49
|
+
::RSpec::Mocks.allow_message(self, message_or_hash, opts, &block)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def unstub(message)
|
54
|
+
::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
|
55
|
+
::RSpec::Mocks.space.proxy_for(self).remove_stub(message)
|
56
|
+
end
|
57
|
+
|
58
|
+
def stub_chain(*chain, &blk)
|
59
|
+
::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
|
60
|
+
::RSpec::Mocks::StubChain.stub_chain_on(self, *chain, &blk)
|
61
|
+
end
|
62
|
+
|
63
|
+
def as_null_object
|
64
|
+
::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
|
65
|
+
@_null_object = true
|
66
|
+
::RSpec::Mocks.proxy_for(self).as_null_object
|
67
|
+
end
|
68
|
+
|
69
|
+
def null_object?
|
70
|
+
::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
|
71
|
+
defined?(@_null_object)
|
72
|
+
end
|
73
|
+
|
74
|
+
def received_message?(message, *args, &block)
|
75
|
+
::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
|
76
|
+
::RSpec::Mocks.proxy_for(self).received_message?(message, *args, &block)
|
77
|
+
end
|
78
|
+
|
79
|
+
unless Class.respond_to? :any_instance
|
80
|
+
Class.class_exec do
|
81
|
+
def any_instance
|
82
|
+
::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__)
|
83
|
+
::RSpec::Mocks.any_instance_recorder_for(self)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# @api private
|
91
|
+
# Disables the should syntax (`dbl.stub`, `dbl.should_receive`, etc).
|
92
|
+
def self.disable_should(syntax_host = default_should_syntax_host)
|
93
|
+
return unless should_enabled?(syntax_host)
|
94
|
+
|
95
|
+
syntax_host.class_exec do
|
96
|
+
undef should_receive
|
97
|
+
undef should_not_receive
|
98
|
+
undef stub
|
99
|
+
undef unstub
|
100
|
+
undef stub_chain
|
101
|
+
undef as_null_object
|
102
|
+
undef null_object?
|
103
|
+
undef received_message?
|
104
|
+
end
|
105
|
+
|
106
|
+
Class.class_exec do
|
107
|
+
undef any_instance
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# @api private
|
112
|
+
# Enables the expect syntax (`expect(dbl).to receive`, `allow(dbl).to receive`, etc).
|
113
|
+
def self.enable_expect(syntax_host = ::RSpec::Mocks::ExampleMethods)
|
114
|
+
return if expect_enabled?(syntax_host)
|
115
|
+
|
116
|
+
syntax_host.class_exec do
|
117
|
+
def receive(method_name, &block)
|
118
|
+
Matchers::Receive.new(method_name, block)
|
119
|
+
end
|
120
|
+
|
121
|
+
def receive_messages(message_return_value_hash)
|
122
|
+
matcher = Matchers::ReceiveMessages.new(message_return_value_hash)
|
123
|
+
matcher.warn_about_block if block_given?
|
124
|
+
matcher
|
125
|
+
end
|
126
|
+
|
127
|
+
def allow(target)
|
128
|
+
AllowanceTarget.new(target)
|
129
|
+
end
|
130
|
+
|
131
|
+
def expect_any_instance_of(klass)
|
132
|
+
AnyInstanceExpectationTarget.new(klass)
|
133
|
+
end
|
134
|
+
|
135
|
+
def allow_any_instance_of(klass)
|
136
|
+
AnyInstanceAllowanceTarget.new(klass)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
RSpec::Mocks::ExampleMethods::ExpectHost.class_exec do
|
141
|
+
def expect(target)
|
142
|
+
ExpectationTarget.new(target)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# @api private
|
148
|
+
# Disables the expect syntax (`expect(dbl).to receive`, `allow(dbl).to receive`, etc).
|
149
|
+
def self.disable_expect(syntax_host = ::RSpec::Mocks::ExampleMethods)
|
150
|
+
return unless expect_enabled?(syntax_host)
|
151
|
+
|
152
|
+
syntax_host.class_exec do
|
153
|
+
undef receive
|
154
|
+
undef receive_messages
|
155
|
+
undef allow
|
156
|
+
undef expect_any_instance_of
|
157
|
+
undef allow_any_instance_of
|
158
|
+
end
|
159
|
+
|
160
|
+
RSpec::Mocks::ExampleMethods::ExpectHost.class_exec do
|
161
|
+
undef expect
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
# @api private
|
166
|
+
# Indicates whether or not the should syntax is enabled.
|
167
|
+
def self.should_enabled?(syntax_host = default_should_syntax_host)
|
168
|
+
syntax_host.method_defined?(:should_receive)
|
169
|
+
end
|
170
|
+
|
171
|
+
# @api private
|
172
|
+
# Indicates whether or not the expect syntax is enabled.
|
173
|
+
def self.expect_enabled?(syntax_host = ::RSpec::Mocks::ExampleMethods)
|
174
|
+
syntax_host.method_defined?(:allow)
|
175
|
+
end
|
176
|
+
|
177
|
+
# @api private
|
178
|
+
# Determines where the methods like `should_receive`, and `stub` are added.
|
179
|
+
def self.default_should_syntax_host
|
180
|
+
# JRuby 1.7.4 introduces a regression whereby `defined?(::BasicObject) => nil`
|
181
|
+
# yet `BasicObject` still exists and patching onto ::Object breaks things
|
182
|
+
# e.g. SimpleDelegator expectations won't work
|
183
|
+
#
|
184
|
+
# See: https://github.com/jruby/jruby/issues/814
|
185
|
+
if defined?(JRUBY_VERSION) && JRUBY_VERSION == '1.7.4' && RUBY_VERSION.to_f > 1.8
|
186
|
+
return ::BasicObject
|
187
|
+
end
|
188
|
+
|
189
|
+
# On 1.8.7, Object.ancestors.last == Kernel but
|
190
|
+
# things blow up if we include `RSpec::Mocks::Methods`
|
191
|
+
# into Kernel...not sure why.
|
192
|
+
return Object unless defined?(::BasicObject)
|
193
|
+
|
194
|
+
# MacRuby has BasicObject but it's not the root class.
|
195
|
+
return Object unless Object.ancestors.last == ::BasicObject
|
196
|
+
|
197
|
+
::BasicObject
|
198
|
+
end
|
199
|
+
|
200
|
+
# @method should_receive
|
201
|
+
# Sets an expectation that this object should receive a message before
|
202
|
+
# the end of the example.
|
203
|
+
#
|
204
|
+
# @example
|
205
|
+
#
|
206
|
+
# logger = double('logger')
|
207
|
+
# thing_that_logs = ThingThatLogs.new(logger)
|
208
|
+
# logger.should_receive(:log)
|
209
|
+
# thing_that_logs.do_something_that_logs_a_message
|
210
|
+
#
|
211
|
+
# @note This is only available when you have enabled the `should` syntax.
|
212
|
+
|
213
|
+
# @method should_not_receive
|
214
|
+
# Sets and expectation that this object should _not_ receive a message
|
215
|
+
# during this example.
|
216
|
+
|
217
|
+
# @method stub
|
218
|
+
# Tells the object to respond to the message with the specified value.
|
219
|
+
#
|
220
|
+
# @example
|
221
|
+
#
|
222
|
+
# counter.stub(:count).and_return(37)
|
223
|
+
# counter.stub(:count => 37)
|
224
|
+
# counter.stub(:count) { 37 }
|
225
|
+
#
|
226
|
+
# @note This is only available when you have enabled the `should` syntax.
|
227
|
+
|
228
|
+
# @method unstub
|
229
|
+
# Removes a stub. On a double, the object will no longer respond to
|
230
|
+
# `message`. On a real object, the original method (if it exists) is
|
231
|
+
# restored.
|
232
|
+
#
|
233
|
+
# This is rarely used, but can be useful when a stub is set up during a
|
234
|
+
# shared `before` hook for the common case, but you want to replace it
|
235
|
+
# for a special case.
|
236
|
+
#
|
237
|
+
# @note This is only available when you have enabled the `should` syntax.
|
238
|
+
|
239
|
+
# @method stub_chain
|
240
|
+
# @overload stub_chain(method1, method2)
|
241
|
+
# @overload stub_chain("method1.method2")
|
242
|
+
# @overload stub_chain(method1, method_to_value_hash)
|
243
|
+
#
|
244
|
+
# Stubs a chain of methods.
|
245
|
+
#
|
246
|
+
# ## Warning:
|
247
|
+
#
|
248
|
+
# Chains can be arbitrarily long, which makes it quite painless to
|
249
|
+
# violate the Law of Demeter in violent ways, so you should consider any
|
250
|
+
# use of `stub_chain` a code smell. Even though not all code smells
|
251
|
+
# indicate real problems (think fluent interfaces), `stub_chain` still
|
252
|
+
# results in brittle examples. For example, if you write
|
253
|
+
# `foo.stub_chain(:bar, :baz => 37)` in a spec and then the
|
254
|
+
# implementation calls `foo.baz.bar`, the stub will not work.
|
255
|
+
#
|
256
|
+
# @example
|
257
|
+
#
|
258
|
+
# double.stub_chain("foo.bar") { :baz }
|
259
|
+
# double.stub_chain(:foo, :bar => :baz)
|
260
|
+
# double.stub_chain(:foo, :bar) { :baz }
|
261
|
+
#
|
262
|
+
# # Given any of ^^ these three forms ^^:
|
263
|
+
# double.foo.bar # => :baz
|
264
|
+
#
|
265
|
+
# # Common use in Rails/ActiveRecord:
|
266
|
+
# Article.stub_chain("recent.published") { [Article.new] }
|
267
|
+
#
|
268
|
+
# @note This is only available when you have enabled the `should` syntax.
|
269
|
+
|
270
|
+
# @method as_null_object
|
271
|
+
# Tells the object to respond to all messages. If specific stub values
|
272
|
+
# are declared, they'll work as expected. If not, the receiver is
|
273
|
+
# returned.
|
274
|
+
#
|
275
|
+
# @note This is only available when you have enabled the `should` syntax.
|
276
|
+
|
277
|
+
# @method null_object?
|
278
|
+
# Returns true if this object has received `as_null_object`
|
279
|
+
#
|
280
|
+
# @note This is only available when you have enabled the `should` syntax.
|
281
|
+
|
282
|
+
# @method any_instance
|
283
|
+
# Used to set stubs and message expectations on any instance of a given
|
284
|
+
# class. Returns a [Recorder](Recorder), which records messages like
|
285
|
+
# `stub` and `should_receive` for later playback on instances of the
|
286
|
+
# class.
|
287
|
+
#
|
288
|
+
# @example
|
289
|
+
#
|
290
|
+
# Car.any_instance.should_receive(:go)
|
291
|
+
# race = Race.new
|
292
|
+
# race.cars << Car.new
|
293
|
+
# race.go # assuming this delegates to all of its cars
|
294
|
+
# # this example would pass
|
295
|
+
#
|
296
|
+
# Account.any_instance.stub(:balance) { Money.new(:USD, 25) }
|
297
|
+
# Account.new.balance # => Money.new(:USD, 25))
|
298
|
+
#
|
299
|
+
# @return [Recorder]
|
300
|
+
#
|
301
|
+
# @note This is only available when you have enabled the `should` syntax.
|
302
|
+
|
303
|
+
# @method expect
|
304
|
+
# Used to wrap an object in preparation for setting a mock expectation
|
305
|
+
# on it.
|
306
|
+
#
|
307
|
+
# @example
|
308
|
+
#
|
309
|
+
# expect(obj).to receive(:foo).with(5).and_return(:return_value)
|
310
|
+
#
|
311
|
+
# @note This method is usually provided by rspec-expectations, unless
|
312
|
+
# you are using rspec-mocks w/o rspec-expectations, in which case it
|
313
|
+
# is only made available if you enable the `expect` syntax.
|
314
|
+
|
315
|
+
# @method allow
|
316
|
+
# Used to wrap an object in preparation for stubbing a method
|
317
|
+
# on it.
|
318
|
+
#
|
319
|
+
# @example
|
320
|
+
#
|
321
|
+
# allow(dbl).to receive(:foo).with(5).and_return(:return_value)
|
322
|
+
#
|
323
|
+
# @note This is only available when you have enabled the `expect` syntax.
|
324
|
+
|
325
|
+
# @method expect_any_instance_of
|
326
|
+
# Used to wrap a class in preparation for setting a mock expectation
|
327
|
+
# on instances of it.
|
328
|
+
#
|
329
|
+
# @example
|
330
|
+
#
|
331
|
+
# expect_any_instance_of(MyClass).to receive(:foo)
|
332
|
+
#
|
333
|
+
# @note This is only available when you have enabled the `expect` syntax.
|
334
|
+
|
335
|
+
# @method allow_any_instance_of
|
336
|
+
# Used to wrap a class in preparation for stubbing a method
|
337
|
+
# on instances of it.
|
338
|
+
#
|
339
|
+
# @example
|
340
|
+
#
|
341
|
+
# allow_any_instance_of(MyClass).to receive(:foo)
|
342
|
+
#
|
343
|
+
# @note This is only available when you have enabled the `expect` syntax.
|
344
|
+
|
345
|
+
# @method receive
|
346
|
+
# Used to specify a message that you expect or allow an object
|
347
|
+
# to receive. The object returned by `receive` supports the same
|
348
|
+
# fluent interface that `should_receive` and `stub` have always
|
349
|
+
# supported, allowing you to constrain the arguments or number of
|
350
|
+
# times, and configure how the object should respond to the message.
|
351
|
+
#
|
352
|
+
# @example
|
353
|
+
#
|
354
|
+
# expect(obj).to receive(:hello).with("world").exactly(3).times
|
355
|
+
#
|
356
|
+
# @note This is only available when you have enabled the `expect` syntax.
|
357
|
+
#
|
358
|
+
# @method receive_messages
|
359
|
+
# Shorthand syntax used to setup message(s), and their return value(s),
|
360
|
+
# that you expect or allow an object to receive. The method takes a hash
|
361
|
+
# of messages and their respective return values. Unlike with `receive`,
|
362
|
+
# you cannot apply further customizations using a block or the fluent
|
363
|
+
# interface.
|
364
|
+
#
|
365
|
+
# @example
|
366
|
+
#
|
367
|
+
# allow(obj).to receive_messages(:speak => "Hello World")
|
368
|
+
# allow(obj).to receive_messages(:speak => "Hello", :meow => "Meow")
|
369
|
+
#
|
370
|
+
# @note This is only available when you have enabled the `expect` syntax.
|
371
|
+
end
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Mocks
|
3
|
+
UnsupportedMatcherError = Class.new(StandardError)
|
4
|
+
NegationUnsupportedError = Class.new(StandardError)
|
5
|
+
|
6
|
+
class TargetBase
|
7
|
+
def initialize(target)
|
8
|
+
@target = target
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.delegate_to(matcher_method)
|
12
|
+
define_method(:to) do |matcher, &block|
|
13
|
+
unless Matchers::Receive === matcher || Matchers::ReceiveMessages === matcher
|
14
|
+
raise_unsupported_matcher(:to, matcher)
|
15
|
+
end
|
16
|
+
define_matcher(matcher, matcher_method, &block)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.delegate_not_to(matcher_method, options = {})
|
21
|
+
method_name = options.fetch(:from)
|
22
|
+
define_method(method_name) do |matcher, &block|
|
23
|
+
case matcher
|
24
|
+
when Matchers::Receive then define_matcher(matcher, matcher_method, &block)
|
25
|
+
when Matchers::ReceiveMessages then raise_negation_unsupported(method_name, matcher)
|
26
|
+
else
|
27
|
+
raise_unsupported_matcher(method_name, matcher)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.disallow_negation(method_name)
|
33
|
+
define_method(method_name) do |matcher, *args|
|
34
|
+
raise_negation_unsupported(method_name, matcher)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def define_matcher(matcher, name, &block)
|
41
|
+
matcher.__send__(name, @target, &block)
|
42
|
+
end
|
43
|
+
|
44
|
+
def raise_unsupported_matcher(method_name, matcher)
|
45
|
+
raise UnsupportedMatcherError,
|
46
|
+
"only the `receive` or `receive_messages` matchers are supported " +
|
47
|
+
"with `#{expression}(...).#{method_name}`, but you have provided: #{matcher}"
|
48
|
+
end
|
49
|
+
|
50
|
+
def raise_negation_unsupported(method_name, matcher)
|
51
|
+
raise NegationUnsupportedError,
|
52
|
+
"`#{expression}(...).#{method_name} #{matcher.name}` is not supported since it " +
|
53
|
+
"doesn't really make sense. What would it even mean?"
|
54
|
+
end
|
55
|
+
|
56
|
+
def expression
|
57
|
+
self.class::EXPRESSION
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class AllowanceTarget < TargetBase
|
62
|
+
EXPRESSION = :allow
|
63
|
+
delegate_to :setup_allowance
|
64
|
+
disallow_negation :not_to
|
65
|
+
disallow_negation :to_not
|
66
|
+
end
|
67
|
+
|
68
|
+
class ExpectationTarget < TargetBase
|
69
|
+
EXPRESSION = :expect
|
70
|
+
delegate_to :setup_expectation
|
71
|
+
delegate_not_to :setup_negative_expectation, :from => :not_to
|
72
|
+
delegate_not_to :setup_negative_expectation, :from => :to_not
|
73
|
+
end
|
74
|
+
|
75
|
+
class AnyInstanceAllowanceTarget < TargetBase
|
76
|
+
EXPRESSION = :allow_any_instance_of
|
77
|
+
delegate_to :setup_any_instance_allowance
|
78
|
+
disallow_negation :not_to
|
79
|
+
disallow_negation :to_not
|
80
|
+
end
|
81
|
+
|
82
|
+
class AnyInstanceExpectationTarget < TargetBase
|
83
|
+
EXPRESSION = :expect_any_instance_of
|
84
|
+
delegate_to :setup_any_instance_expectation
|
85
|
+
delegate_not_to :setup_any_instance_negative_expectation, :from => :not_to
|
86
|
+
delegate_not_to :setup_any_instance_negative_expectation, :from => :to_not
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
@@ -0,0 +1,109 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Mocks
|
3
|
+
# Implements the methods needed for a pure test double. RSpec::Mocks::Mock
|
4
|
+
# includes this module, and it is provided for cases where you want a
|
5
|
+
# pure test double without subclassing RSpec::Mocks::Mock.
|
6
|
+
module TestDouble
|
7
|
+
# Extends the TestDouble module onto the given object and
|
8
|
+
# initializes it as a test double.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
#
|
12
|
+
# module = Module.new
|
13
|
+
# RSpec::Mocks::TestDouble.extend_onto(module, "MyMixin", :foo => "bar")
|
14
|
+
# module.foo #=> "bar"
|
15
|
+
def self.extend_onto(object, name=nil, stubs={})
|
16
|
+
object.extend self
|
17
|
+
object.send(:__initialize_as_test_double, name, stubs)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Creates a new test double with a `name` (that will be used in error
|
21
|
+
# messages only)
|
22
|
+
def initialize(name=nil, stubs={})
|
23
|
+
__initialize_as_test_double(name, stubs)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Tells the object to respond to all messages. If specific stub values
|
27
|
+
# are declared, they'll work as expected. If not, the receiver is
|
28
|
+
# returned.
|
29
|
+
def as_null_object
|
30
|
+
__mock_proxy.as_null_object
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns true if this object has received `as_null_object`
|
34
|
+
def null_object?
|
35
|
+
__mock_proxy.null_object?
|
36
|
+
end
|
37
|
+
|
38
|
+
# This allows for comparing the mock to other objects that proxy such as
|
39
|
+
# ActiveRecords belongs_to proxy objects. By making the other object run
|
40
|
+
# the comparison, we're sure the call gets delegated to the proxy
|
41
|
+
# target.
|
42
|
+
def ==(other)
|
43
|
+
other == __mock_proxy
|
44
|
+
end
|
45
|
+
|
46
|
+
# @private
|
47
|
+
def inspect
|
48
|
+
"#<#{self.class}:#{sprintf '0x%x', self.object_id} @name=#{@name.inspect}>"
|
49
|
+
end
|
50
|
+
|
51
|
+
# @private
|
52
|
+
def to_s
|
53
|
+
inspect.gsub('<','[').gsub('>',']')
|
54
|
+
end
|
55
|
+
|
56
|
+
alias_method :to_str, :to_s
|
57
|
+
|
58
|
+
# @private
|
59
|
+
def respond_to?(message, incl_private=false)
|
60
|
+
__mock_proxy.null_object? ? true : super
|
61
|
+
end
|
62
|
+
|
63
|
+
# @private
|
64
|
+
def __build_mock_proxy(order_group)
|
65
|
+
Proxy.new(self, order_group, @name)
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def __initialize_as_test_double(name=nil, stubs={})
|
71
|
+
if Hash === name && stubs.empty?
|
72
|
+
stubs = name
|
73
|
+
@name = nil
|
74
|
+
else
|
75
|
+
@name = name
|
76
|
+
end
|
77
|
+
assign_stubs(stubs)
|
78
|
+
end
|
79
|
+
|
80
|
+
def method_missing(message, *args, &block)
|
81
|
+
if __mock_proxy.null_object?
|
82
|
+
case message
|
83
|
+
when :to_int then return 0
|
84
|
+
when :to_a, :to_ary then return nil
|
85
|
+
end
|
86
|
+
end
|
87
|
+
__mock_proxy.record_message_received(message, *args, &block)
|
88
|
+
|
89
|
+
begin
|
90
|
+
__mock_proxy.null_object? ? self : super
|
91
|
+
rescue NameError
|
92
|
+
# Required wrapping doubles in an Array on Ruby 1.9.2
|
93
|
+
raise NoMethodError if [:to_a, :to_ary].include? message
|
94
|
+
__mock_proxy.raise_unexpected_message_error(message, *args)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def assign_stubs(stubs)
|
99
|
+
stubs.each_pair do |message, response|
|
100
|
+
__mock_proxy.add_simple_stub(message, response)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def __mock_proxy
|
105
|
+
::RSpec::Mocks.proxy_for(self)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'rspec/mocks/mock'
|
2
|
+
require 'rspec/mocks/verifying_proxy'
|
3
|
+
|
4
|
+
module RSpec
|
5
|
+
module Mocks
|
6
|
+
|
7
|
+
module VerifyingDouble
|
8
|
+
def method_missing(message, *args, &block)
|
9
|
+
# Null object conditional is an optimization. If not a null object,
|
10
|
+
# validity of method expectations will have been checked at definition
|
11
|
+
# time.
|
12
|
+
__mock_proxy.ensure_implemented(message) if null_object?
|
13
|
+
super
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# A mock providing a custom proxy that can verify the validity of any
|
18
|
+
# method stubs or expectations against the public instance methods of the
|
19
|
+
# given class.
|
20
|
+
class InstanceVerifyingDouble
|
21
|
+
include TestDouble
|
22
|
+
include VerifyingDouble
|
23
|
+
|
24
|
+
def initialize(doubled_module, *args)
|
25
|
+
@doubled_module = doubled_module
|
26
|
+
|
27
|
+
__initialize_as_test_double(doubled_module, *args)
|
28
|
+
end
|
29
|
+
|
30
|
+
def __build_mock_proxy(order_group)
|
31
|
+
VerifyingProxy.new(self, order_group,
|
32
|
+
@doubled_module,
|
33
|
+
InstanceMethodReference
|
34
|
+
)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# An awkward module necessary because we cannot otherwise have
|
39
|
+
# ClassVerifyingDouble inherit from Module and still share these methods.
|
40
|
+
module ObjectVerifyingDoubleMethods
|
41
|
+
include TestDouble
|
42
|
+
include VerifyingDouble
|
43
|
+
|
44
|
+
def initialize(doubled_module, *args)
|
45
|
+
@doubled_module = doubled_module
|
46
|
+
|
47
|
+
__initialize_as_test_double(doubled_module, *args)
|
48
|
+
end
|
49
|
+
|
50
|
+
def __build_mock_proxy(order_group)
|
51
|
+
VerifyingProxy.new(self, order_group,
|
52
|
+
@doubled_module,
|
53
|
+
ObjectMethodReference
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
def as_stubbed_const(options = {})
|
58
|
+
ConstantMutator.stub(@doubled_module.const_to_replace, self, options)
|
59
|
+
self
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Similar to an InstanceVerifyingDouble, except that it verifies against
|
64
|
+
# public methods of the given object.
|
65
|
+
class ObjectVerifyingDouble
|
66
|
+
include ObjectVerifyingDoubleMethods
|
67
|
+
end
|
68
|
+
|
69
|
+
# Effectively the same as an ObjectVerifyingDouble (since a class is a type
|
70
|
+
# of object), except with Module in the inheritance chain so that
|
71
|
+
# transferring nested constants to work.
|
72
|
+
class ClassVerifyingDouble < Module
|
73
|
+
include ObjectVerifyingDoubleMethods
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rspec/mocks/arity_calculator'
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Mocks
|
5
|
+
|
6
|
+
# A message expectation that knows about the real implementation of the
|
7
|
+
# message being expected, so that it can verify that any expectations
|
8
|
+
# have the correct arity.
|
9
|
+
class VerifyingMessageExpectation < MessageExpectation
|
10
|
+
|
11
|
+
# A level of indirection is used here rather than just passing in the
|
12
|
+
# method itself, since method look up is expensive and we only want to
|
13
|
+
# do it if actually needed.
|
14
|
+
#
|
15
|
+
# Conceptually the method reference makes more sense as a constructor
|
16
|
+
# argument since it should be immutable, but it is significantly more
|
17
|
+
# straight forward to build the object in pieces so for now it stays as
|
18
|
+
# an accessor.
|
19
|
+
attr_accessor :method_reference
|
20
|
+
|
21
|
+
def initialize(*args)
|
22
|
+
super
|
23
|
+
end
|
24
|
+
|
25
|
+
# @override
|
26
|
+
def with(*args, &block)
|
27
|
+
unless ArgumentMatchers::AnyArgsMatcher === args.first
|
28
|
+
expected_arity = if ArgumentMatchers::NoArgsMatcher === args.first
|
29
|
+
0
|
30
|
+
elsif args.length > 0
|
31
|
+
args.length
|
32
|
+
else
|
33
|
+
# No arguments given, this will raise.
|
34
|
+
super
|
35
|
+
end
|
36
|
+
|
37
|
+
ensure_arity!(expected_arity)
|
38
|
+
end
|
39
|
+
super
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def ensure_arity!(actual)
|
45
|
+
return if method_reference.nil?
|
46
|
+
|
47
|
+
method_reference.when_defined do |method|
|
48
|
+
calculator = ArityCalculator.new(method)
|
49
|
+
unless calculator.within_range?(actual)
|
50
|
+
# Fail fast is required, otherwise the message expecation will fail
|
51
|
+
# as well ("expected method not called") and clobber this one.
|
52
|
+
@failed_fast = true
|
53
|
+
@error_generator.raise_arity_error(calculator, actual)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|