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,201 @@
|
|
1
|
+
require 'rspec/mocks/object_reference'
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Mocks
|
5
|
+
module ExampleMethods
|
6
|
+
include RSpec::Mocks::ArgumentMatchers
|
7
|
+
|
8
|
+
# @overload double()
|
9
|
+
# @overload double(name)
|
10
|
+
# @overload double(stubs)
|
11
|
+
# @overload double(name, stubs)
|
12
|
+
# @param name [String/Symbol] (optional) used in
|
13
|
+
# clarify intent
|
14
|
+
# @param stubs (Hash) (optional) hash of message/return-value pairs
|
15
|
+
# @return (Mock)
|
16
|
+
#
|
17
|
+
# Constructs an instance of [RSpec::Mocks::Mock](RSpec::Mocks::Mock) configured
|
18
|
+
# with an optional name, used for reporting in failure messages, and an optional
|
19
|
+
# hash of message/return-value pairs.
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
#
|
23
|
+
# book = double("book", :title => "The RSpec Book")
|
24
|
+
# book.title #=> "The RSpec Book"
|
25
|
+
#
|
26
|
+
# card = double("card", :suit => "Spades", :rank => "A")
|
27
|
+
# card.suit #=> "Spades"
|
28
|
+
# card.rank #=> "A"
|
29
|
+
#
|
30
|
+
def double(*args)
|
31
|
+
declare_double(Mock, *args)
|
32
|
+
end
|
33
|
+
|
34
|
+
# @overload instance_double(doubled_class)
|
35
|
+
# @overload instance_double(doubled_class, stubs)
|
36
|
+
# @param doubled_class [String, Class]
|
37
|
+
# @param stubs [Hash] (optional) hash of message/return-value pairs
|
38
|
+
# @return InstanceVerifyingDouble
|
39
|
+
#
|
40
|
+
# Constructs a test double against a specific class. If the given class
|
41
|
+
# name has been loaded, only instance methods defined on the class are
|
42
|
+
# allowed to be stubbed. In all other ways it behaves like a
|
43
|
+
# [double](double).
|
44
|
+
def instance_double(doubled_class, *args)
|
45
|
+
ref = ObjectReference.for(doubled_class)
|
46
|
+
declare_verifying_double(InstanceVerifyingDouble, ref, *args)
|
47
|
+
end
|
48
|
+
|
49
|
+
# @overload class_double(doubled_class)
|
50
|
+
# @overload class_double(doubled_class, stubs)
|
51
|
+
# @param doubled_class [String, Module]
|
52
|
+
# @param stubs [Hash] (optional) hash of message/return-value pairs
|
53
|
+
# @return ClassVerifyingDouble
|
54
|
+
#
|
55
|
+
# Constructs a test double against a specific class. If the given class
|
56
|
+
# name has been loaded, only class methods defined on the class are
|
57
|
+
# allowed to be stubbed. In all other ways it behaves like a
|
58
|
+
# [double](double).
|
59
|
+
def class_double(doubled_class, *args)
|
60
|
+
ref = ObjectReference.for(doubled_class)
|
61
|
+
declare_verifying_double(ClassVerifyingDouble, ref, *args)
|
62
|
+
end
|
63
|
+
|
64
|
+
# @overload object_double(object_or_name)
|
65
|
+
# @overload object_double(object_or_name, stubs)
|
66
|
+
# @param object_or_name [String, Object]
|
67
|
+
# @param stubs [Hash] (optional) hash of message/return-value pairs
|
68
|
+
# @return ObjectVerifyingDouble
|
69
|
+
#
|
70
|
+
# Constructs a test double against a specific object. Only the methods
|
71
|
+
# the object responds to are allowed to be stubbed. If a String argument
|
72
|
+
# is provided, it is assumed to reference a constant object which is used
|
73
|
+
# for verification. In all other ways it behaves like a [double](double).
|
74
|
+
def object_double(object_or_name, *args)
|
75
|
+
ref = ObjectReference.for(object_or_name, :allow_direct_object_refs)
|
76
|
+
declare_verifying_double(ObjectVerifyingDouble, ref, *args)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Disables warning messages about expectations being set on nil.
|
80
|
+
#
|
81
|
+
# By default warning messages are issued when expectations are set on
|
82
|
+
# nil. This is to prevent false-positives and to catch potential bugs
|
83
|
+
# early on.
|
84
|
+
def allow_message_expectations_on_nil
|
85
|
+
RSpec::Mocks.space.proxy_for(nil).warn_about_expectations = false
|
86
|
+
end
|
87
|
+
|
88
|
+
# Stubs the named constant with the given value.
|
89
|
+
# Like method stubs, the constant will be restored
|
90
|
+
# to its original value (or lack of one, if it was
|
91
|
+
# undefined) when the example completes.
|
92
|
+
#
|
93
|
+
# @param constant_name [String] The fully qualified name of the constant. The current
|
94
|
+
# constant scoping at the point of call is not considered.
|
95
|
+
# @param value [Object] The value to make the constant refer to. When the
|
96
|
+
# example completes, the constant will be restored to its prior state.
|
97
|
+
# @param options [Hash] Stubbing options.
|
98
|
+
# @option options :transfer_nested_constants [Boolean, Array<Symbol>] Determines
|
99
|
+
# what nested constants, if any, will be transferred from the original value
|
100
|
+
# of the constant to the new value of the constant. This only works if both
|
101
|
+
# the original and new values are modules (or classes).
|
102
|
+
# @return [Object] the stubbed value of the constant
|
103
|
+
#
|
104
|
+
# @example
|
105
|
+
#
|
106
|
+
# stub_const("MyClass", Class.new) # => Replaces (or defines) MyClass with a new class object.
|
107
|
+
# stub_const("SomeModel::PER_PAGE", 5) # => Sets SomeModel::PER_PAGE to 5.
|
108
|
+
#
|
109
|
+
# class CardDeck
|
110
|
+
# SUITS = [:Spades, :Diamonds, :Clubs, :Hearts]
|
111
|
+
# NUM_CARDS = 52
|
112
|
+
# end
|
113
|
+
#
|
114
|
+
# stub_const("CardDeck", Class.new)
|
115
|
+
# CardDeck::SUITS # => uninitialized constant error
|
116
|
+
# CardDeck::NUM_CARDS # => uninitialized constant error
|
117
|
+
#
|
118
|
+
# stub_const("CardDeck", Class.new, :transfer_nested_constants => true)
|
119
|
+
# CardDeck::SUITS # => our suits array
|
120
|
+
# CardDeck::NUM_CARDS # => 52
|
121
|
+
#
|
122
|
+
# stub_const("CardDeck", Class.new, :transfer_nested_constants => [:SUITS])
|
123
|
+
# CardDeck::SUITS # => our suits array
|
124
|
+
# CardDeck::NUM_CARDS # => uninitialized constant error
|
125
|
+
def stub_const(constant_name, value, options = {})
|
126
|
+
ConstantMutator.stub(constant_name, value, options)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Hides the named constant with the given value. The constant will be
|
130
|
+
# undefined for the duration of the test.
|
131
|
+
#
|
132
|
+
# Like method stubs, the constant will be restored to its original value
|
133
|
+
# when the example completes.
|
134
|
+
#
|
135
|
+
# @param constant_name [String] The fully qualified name of the constant.
|
136
|
+
# The current constant scoping at the point of call is not considered.
|
137
|
+
#
|
138
|
+
# @example
|
139
|
+
#
|
140
|
+
# hide_const("MyClass") # => MyClass is now an undefined constant
|
141
|
+
def hide_const(constant_name)
|
142
|
+
ConstantMutator.hide(constant_name)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Verifies that the given object received the expected message during the
|
146
|
+
# course of the test. The method must have previously been stubbed in
|
147
|
+
# order for messages to be verified.
|
148
|
+
#
|
149
|
+
# Stubbing and verifying messages received in this way implements the
|
150
|
+
# Test Spy pattern.
|
151
|
+
#
|
152
|
+
# @param method_name [Symbol] name of the method expected to have been
|
153
|
+
# called.
|
154
|
+
#
|
155
|
+
# @example
|
156
|
+
#
|
157
|
+
# invitation = double('invitation', accept: true)
|
158
|
+
# user.accept_invitation(invitation)
|
159
|
+
# expect(invitation).to have_received(:accept)
|
160
|
+
#
|
161
|
+
# # You can also use most message expectations:
|
162
|
+
# expect(invitation).to have_received(:accept).with(mailer).once
|
163
|
+
def have_received(method_name, &block)
|
164
|
+
Matchers::HaveReceived.new(method_name, &block)
|
165
|
+
end
|
166
|
+
|
167
|
+
def self.included(klass)
|
168
|
+
klass.class_exec do
|
169
|
+
# This gets mixed in so that if `RSpec::Matchers` is included in
|
170
|
+
# `klass` later, it's definition of `expect` will take precedence.
|
171
|
+
include ExpectHost unless method_defined?(:expect)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
private
|
176
|
+
|
177
|
+
def declare_verifying_double(type, ref, *args)
|
178
|
+
if RSpec::Mocks.configuration.verify_doubled_constant_names? &&
|
179
|
+
!ref.defined?
|
180
|
+
|
181
|
+
raise NameError,
|
182
|
+
"#{ref.name} is not a defined constant. " +
|
183
|
+
"Perhaps you misspelt it? " +
|
184
|
+
"Disable check with verify_doubled_constant_names configuration option."
|
185
|
+
end
|
186
|
+
|
187
|
+
declare_double(type, ref, *args)
|
188
|
+
end
|
189
|
+
|
190
|
+
def declare_double(type, *args)
|
191
|
+
args << {} unless Hash === args.last
|
192
|
+
type.new(*args)
|
193
|
+
end
|
194
|
+
|
195
|
+
# This module exists to host the `expect` method for cases where
|
196
|
+
# rspec-mocks is used w/o rspec-expectations.
|
197
|
+
module ExpectHost
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Marshal
|
2
|
+
class << self
|
3
|
+
# Duplicates any mock objects before serialization. Otherwise,
|
4
|
+
# serialization will fail because methods exist on the singleton class.
|
5
|
+
def dump_with_mocks(object, *rest)
|
6
|
+
if ::RSpec::Mocks.space.nil? || !::RSpec::Mocks.space.registered?(object) || NilClass === object
|
7
|
+
dump_without_mocks(object, *rest)
|
8
|
+
else
|
9
|
+
dump_without_mocks(object.dup, *rest)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
alias_method :dump_without_mocks, :dump
|
14
|
+
undef_method :dump
|
15
|
+
alias_method :dump, :dump_with_mocks
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# Require everything except the global extensions of class and object. This
|
2
|
+
# supports wrapping rspec's mocking functionality without invading every
|
3
|
+
# object in the system.
|
4
|
+
|
5
|
+
require 'rspec/support/caller_filter'
|
6
|
+
require 'rspec/support/warnings'
|
7
|
+
require 'rspec/mocks/instance_method_stasher'
|
8
|
+
require 'rspec/mocks/method_double'
|
9
|
+
require 'rspec/mocks/argument_matchers'
|
10
|
+
require 'rspec/mocks/example_methods'
|
11
|
+
require 'rspec/mocks/proxy'
|
12
|
+
require 'rspec/mocks/proxy_for_nil'
|
13
|
+
require 'rspec/mocks/test_double'
|
14
|
+
require 'rspec/mocks/mock'
|
15
|
+
require 'rspec/mocks/argument_list_matcher'
|
16
|
+
require 'rspec/mocks/message_expectation'
|
17
|
+
require 'rspec/mocks/order_group'
|
18
|
+
require 'rspec/mocks/errors'
|
19
|
+
require 'rspec/mocks/error_generator'
|
20
|
+
require 'rspec/mocks/space'
|
21
|
+
require 'rspec/mocks/extensions/marshal'
|
22
|
+
require 'rspec/mocks/any_instance/chain'
|
23
|
+
require 'rspec/mocks/any_instance/stub_chain'
|
24
|
+
require 'rspec/mocks/any_instance/stub_chain_chain'
|
25
|
+
require 'rspec/mocks/any_instance/expectation_chain'
|
26
|
+
require 'rspec/mocks/any_instance/message_chains'
|
27
|
+
require 'rspec/mocks/any_instance/recorder'
|
28
|
+
require 'rspec/mocks/mutate_const'
|
29
|
+
require 'rspec/mocks/matchers/have_received'
|
30
|
+
require 'rspec/mocks/matchers/receive'
|
31
|
+
require 'rspec/mocks/matchers/receive_messages'
|
32
|
+
require 'rspec/mocks/stub_chain'
|
33
|
+
require 'rspec/mocks/targets'
|
34
|
+
require 'rspec/mocks/syntax'
|
35
|
+
require 'rspec/mocks/configuration'
|
36
|
+
require 'rspec/mocks/verifying_double'
|
@@ -0,0 +1,112 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Mocks
|
3
|
+
# @private
|
4
|
+
class InstanceMethodStasher
|
5
|
+
def initialize(object, method)
|
6
|
+
@object = object
|
7
|
+
@method = method
|
8
|
+
@klass = (class << object; self; end)
|
9
|
+
|
10
|
+
@original_method = nil
|
11
|
+
@method_is_stashed = false
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :original_method
|
15
|
+
|
16
|
+
if RUBY_VERSION.to_f < 1.9
|
17
|
+
# @private
|
18
|
+
def method_is_stashed?
|
19
|
+
@method_is_stashed
|
20
|
+
end
|
21
|
+
|
22
|
+
# @private
|
23
|
+
def stash
|
24
|
+
return if !method_defined_directly_on_klass? || @method_is_stashed
|
25
|
+
|
26
|
+
@klass.__send__(:alias_method, stashed_method_name, @method)
|
27
|
+
@method_is_stashed = true
|
28
|
+
end
|
29
|
+
|
30
|
+
# @private
|
31
|
+
def stashed_method_name
|
32
|
+
"obfuscated_by_rspec_mocks__#{@method}"
|
33
|
+
end
|
34
|
+
private :stashed_method_name
|
35
|
+
|
36
|
+
# @private
|
37
|
+
def restore
|
38
|
+
return unless @method_is_stashed
|
39
|
+
|
40
|
+
if @klass.__send__(:method_defined?, @method)
|
41
|
+
@klass.__send__(:undef_method, @method)
|
42
|
+
end
|
43
|
+
@klass.__send__(:alias_method, @method, stashed_method_name)
|
44
|
+
@klass.__send__(:remove_method, stashed_method_name)
|
45
|
+
@method_is_stashed = false
|
46
|
+
end
|
47
|
+
else
|
48
|
+
|
49
|
+
# @private
|
50
|
+
def method_is_stashed?
|
51
|
+
!!@original_method
|
52
|
+
end
|
53
|
+
|
54
|
+
# @private
|
55
|
+
def stash
|
56
|
+
return if !method_defined_directly_on_klass?
|
57
|
+
@original_method ||= ::RSpec::Mocks.method_handle_for(@object, @method)
|
58
|
+
end
|
59
|
+
|
60
|
+
# @private
|
61
|
+
def restore
|
62
|
+
return unless @original_method
|
63
|
+
|
64
|
+
if @klass.__send__(:method_defined?, @method)
|
65
|
+
@klass.__send__(:undef_method, @method)
|
66
|
+
end
|
67
|
+
|
68
|
+
@klass.__send__(:define_method, @method, @original_method)
|
69
|
+
@original_method = nil
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
# @private
|
76
|
+
def method_defined_directly_on_klass?
|
77
|
+
method_defined_on_klass? && method_owned_by_klass?
|
78
|
+
end
|
79
|
+
|
80
|
+
# @private
|
81
|
+
def method_defined_on_klass?(klass = @klass)
|
82
|
+
klass.method_defined?(@method) || klass.private_method_defined?(@method)
|
83
|
+
end
|
84
|
+
|
85
|
+
def method_owned_by_klass?
|
86
|
+
owner = @klass.instance_method(@method).owner
|
87
|
+
|
88
|
+
# On Ruby 2.0.0+ the owner of a method on a class which has been
|
89
|
+
# `prepend`ed may actually be an instance, e.g.
|
90
|
+
# `#<MyClass:0x007fbb94e3cd10>`, rather than the expected `MyClass`.
|
91
|
+
owner = owner.class unless Module === owner
|
92
|
+
|
93
|
+
# On some 1.9s (e.g. rubinius) aliased methods
|
94
|
+
# can report the wrong owner. Example:
|
95
|
+
# class MyClass
|
96
|
+
# class << self
|
97
|
+
# alias alternate_new new
|
98
|
+
# end
|
99
|
+
# end
|
100
|
+
#
|
101
|
+
# MyClass.owner(:alternate_new) returns `Class` when incorrect,
|
102
|
+
# but we need to consider the owner to be `MyClass` because
|
103
|
+
# it is not actually available on `Class` but is on `MyClass`.
|
104
|
+
# Hence, we verify that the owner actually has the method defined.
|
105
|
+
# If the given owner does not have the method defined, we assume
|
106
|
+
# that the method is actually owned by @klass.
|
107
|
+
owner == @klass || !(method_defined_on_klass?(owner))
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Mocks
|
3
|
+
module Matchers
|
4
|
+
class HaveReceived
|
5
|
+
COUNT_CONSTRAINTS = %w(exactly at_least at_most times once twice)
|
6
|
+
ARGS_CONSTRAINTS = %w(with)
|
7
|
+
CONSTRAINTS = COUNT_CONSTRAINTS + ARGS_CONSTRAINTS + %w(ordered)
|
8
|
+
|
9
|
+
def initialize(method_name, &block)
|
10
|
+
@method_name = method_name
|
11
|
+
@block = block
|
12
|
+
@constraints = []
|
13
|
+
@subject = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def name
|
17
|
+
"have_received"
|
18
|
+
end
|
19
|
+
|
20
|
+
def matches?(subject, &block)
|
21
|
+
@block ||= block
|
22
|
+
@subject = subject
|
23
|
+
@expectation = expect
|
24
|
+
expected_messages_received_in_order?
|
25
|
+
end
|
26
|
+
|
27
|
+
def does_not_match?(subject)
|
28
|
+
@subject = subject
|
29
|
+
ensure_count_unconstrained
|
30
|
+
@expectation = expect.never
|
31
|
+
expected_messages_received_in_order?
|
32
|
+
end
|
33
|
+
|
34
|
+
def failure_message
|
35
|
+
generate_failure_message
|
36
|
+
end
|
37
|
+
|
38
|
+
def negative_failure_message
|
39
|
+
generate_failure_message
|
40
|
+
end
|
41
|
+
|
42
|
+
def description
|
43
|
+
expect.description
|
44
|
+
end
|
45
|
+
|
46
|
+
CONSTRAINTS.each do |expectation|
|
47
|
+
define_method expectation do |*args|
|
48
|
+
@constraints << [expectation, *args]
|
49
|
+
self
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def expect
|
56
|
+
expectation = mock_proxy.build_expectation(@method_name)
|
57
|
+
apply_constraints_to expectation
|
58
|
+
expectation
|
59
|
+
end
|
60
|
+
|
61
|
+
def apply_constraints_to(expectation)
|
62
|
+
@constraints.each do |constraint|
|
63
|
+
expectation.send(*constraint)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def ensure_count_unconstrained
|
68
|
+
if count_constraint
|
69
|
+
raise RSpec::Mocks::MockExpectationError,
|
70
|
+
"can't use #{count_constraint} when negative"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def count_constraint
|
75
|
+
@constraints.map(&:first).detect do |constraint|
|
76
|
+
COUNT_CONSTRAINTS.include?(constraint)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def generate_failure_message
|
81
|
+
mock_proxy.check_for_unexpected_arguments(@expectation)
|
82
|
+
@expectation.generate_error
|
83
|
+
rescue RSpec::Mocks::MockExpectationError => error
|
84
|
+
error.message
|
85
|
+
end
|
86
|
+
|
87
|
+
def expected_messages_received_in_order?
|
88
|
+
mock_proxy.replay_received_message_on @expectation, &@block
|
89
|
+
@expectation.expected_messages_received? && @expectation.ensure_expected_ordering_received!
|
90
|
+
end
|
91
|
+
|
92
|
+
def mock_proxy
|
93
|
+
RSpec::Mocks.proxy_for(@subject)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
@@ -0,0 +1,112 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Mocks
|
3
|
+
module Matchers
|
4
|
+
class Receive
|
5
|
+
def initialize(message, block)
|
6
|
+
@message = message
|
7
|
+
@block = block
|
8
|
+
@recorded_customizations = []
|
9
|
+
|
10
|
+
# MRI, JRuby and RBX report the caller inconsistently; MRI
|
11
|
+
# reports an extra "in `new'" line in the backtrace that the
|
12
|
+
# others do not include. The safest way to find the right
|
13
|
+
# line is to search for the first line BEFORE rspec/mocks/syntax.rb.
|
14
|
+
@backtrace_line = CallerFilter.first_non_rspec_line
|
15
|
+
end
|
16
|
+
|
17
|
+
def name
|
18
|
+
"receive"
|
19
|
+
end
|
20
|
+
|
21
|
+
def setup_expectation(subject, &block)
|
22
|
+
warn_if_any_instance("expect", subject)
|
23
|
+
setup_mock_proxy_method_substitute(subject, :add_message_expectation, block)
|
24
|
+
end
|
25
|
+
alias matches? setup_expectation
|
26
|
+
|
27
|
+
def setup_negative_expectation(subject, &block)
|
28
|
+
# ensure `never` goes first for cases like `never.and_return(5)`,
|
29
|
+
# where `and_return` is meant to raise an error
|
30
|
+
@recorded_customizations.unshift Customization.new(:never, [], nil)
|
31
|
+
|
32
|
+
warn_if_any_instance("expect", subject)
|
33
|
+
|
34
|
+
setup_expectation(subject, &block)
|
35
|
+
end
|
36
|
+
alias does_not_match? setup_negative_expectation
|
37
|
+
|
38
|
+
def setup_allowance(subject, &block)
|
39
|
+
warn_if_any_instance("allow", subject)
|
40
|
+
setup_mock_proxy_method_substitute(subject, :add_stub, block)
|
41
|
+
end
|
42
|
+
|
43
|
+
def setup_any_instance_expectation(subject, &block)
|
44
|
+
setup_any_instance_method_substitute(subject, :should_receive, block)
|
45
|
+
end
|
46
|
+
|
47
|
+
def setup_any_instance_negative_expectation(subject, &block)
|
48
|
+
setup_any_instance_method_substitute(subject, :should_not_receive, block)
|
49
|
+
end
|
50
|
+
|
51
|
+
def setup_any_instance_allowance(subject, &block)
|
52
|
+
setup_any_instance_method_substitute(subject, :stub, block)
|
53
|
+
end
|
54
|
+
|
55
|
+
MessageExpectation.public_instance_methods(false).each do |method|
|
56
|
+
next if method_defined?(method)
|
57
|
+
|
58
|
+
define_method(method) do |*args, &block|
|
59
|
+
@recorded_customizations << Customization.new(method, args, block)
|
60
|
+
self
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def warn_if_any_instance(expression, subject)
|
67
|
+
if AnyInstance::Recorder === subject
|
68
|
+
RSpec.warning(
|
69
|
+
"`#{expression}(#{subject.klass}.any_instance).to` " <<
|
70
|
+
"is probably not what you meant, it does not operate on " <<
|
71
|
+
"any instance of `#{subject.klass}`. " <<
|
72
|
+
"Use `#{expression}_any_instance_of(#{subject.klass}).to` instead."
|
73
|
+
)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def setup_mock_proxy_method_substitute(subject, method, block)
|
78
|
+
proxy = ::RSpec::Mocks.proxy_for(subject)
|
79
|
+
setup_method_substitute(proxy, method, block, @backtrace_line)
|
80
|
+
end
|
81
|
+
|
82
|
+
def setup_any_instance_method_substitute(subject, method, block)
|
83
|
+
any_instance_recorder = ::RSpec::Mocks.any_instance_recorder_for(subject)
|
84
|
+
setup_method_substitute(any_instance_recorder, method, block)
|
85
|
+
end
|
86
|
+
|
87
|
+
def setup_method_substitute(host, method, block, *args)
|
88
|
+
args << @message.to_sym
|
89
|
+
expectation = host.__send__(method, *args, &(@block || block))
|
90
|
+
|
91
|
+
@recorded_customizations.each do |customization|
|
92
|
+
customization.playback_onto(expectation)
|
93
|
+
end
|
94
|
+
expectation
|
95
|
+
end
|
96
|
+
|
97
|
+
class Customization
|
98
|
+
def initialize(method_name, args, block)
|
99
|
+
@method_name = method_name
|
100
|
+
@args = args
|
101
|
+
@block = block
|
102
|
+
end
|
103
|
+
|
104
|
+
def playback_onto(expectation)
|
105
|
+
expectation.__send__(@method_name, *@args, &@block)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Mocks
|
3
|
+
module Matchers
|
4
|
+
class ReceiveMessages
|
5
|
+
|
6
|
+
def initialize(message_return_value_hash)
|
7
|
+
@message_return_value_hash = message_return_value_hash
|
8
|
+
@backtrace_line = CallerFilter.first_non_rspec_line
|
9
|
+
end
|
10
|
+
|
11
|
+
def name
|
12
|
+
"receive_messages"
|
13
|
+
end
|
14
|
+
|
15
|
+
def setup_expectation(subject)
|
16
|
+
warn_about_block if block_given?
|
17
|
+
each_message_on( proxy_on(subject) ) do |host, message, return_value|
|
18
|
+
host.add_simple_expectation(message, return_value, @backtrace_line)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
alias matches? setup_expectation
|
22
|
+
|
23
|
+
def setup_negative_expectation(subject)
|
24
|
+
raise NegationUnsupportedError,
|
25
|
+
"`expect(...).to_not receive_messages` is not supported since it " +
|
26
|
+
"doesn't really make sense. What would it even mean?"
|
27
|
+
end
|
28
|
+
alias does_not_match? setup_negative_expectation
|
29
|
+
|
30
|
+
def setup_allowance(subject)
|
31
|
+
warn_about_block if block_given?
|
32
|
+
each_message_on( proxy_on(subject) ) do |host, message, return_value|
|
33
|
+
host.add_simple_stub(message, return_value)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def setup_any_instance_expectation(subject)
|
38
|
+
warn_about_block if block_given?
|
39
|
+
each_message_on( any_instance_of(subject) ) do |host, message, return_value|
|
40
|
+
host.should_receive(message).and_return(return_value)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def setup_any_instance_allowance(subject)
|
45
|
+
warn_about_block if block_given?
|
46
|
+
any_instance_of(subject).stub(@message_return_value_hash)
|
47
|
+
end
|
48
|
+
|
49
|
+
def warn_about_block
|
50
|
+
raise "Implementation blocks aren't supported with `receive_messages`"
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def proxy_on(subject)
|
56
|
+
::RSpec::Mocks.proxy_for(subject)
|
57
|
+
end
|
58
|
+
|
59
|
+
def any_instance_of(subject)
|
60
|
+
::RSpec::Mocks.any_instance_recorder_for(subject)
|
61
|
+
end
|
62
|
+
|
63
|
+
def each_message_on(host)
|
64
|
+
@message_return_value_hash.each do |message, value|
|
65
|
+
yield host, message, value
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|