opal-rspec-cj 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/.gitmodules +15 -0
- data/.travis.yml +13 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +25 -0
- data/Gemfile +8 -0
- data/README.md +147 -0
- data/Rakefile +26 -0
- data/config.ru +10 -0
- data/example/Gemfile +4 -0
- data/example/README.md +13 -0
- data/example/Rakefile +8 -0
- data/example/opal/user.rb +11 -0
- data/example/spec/user_spec.rb +15 -0
- data/lib/opal-rspec.rb +2 -0
- data/lib/opal/rspec.rb +20 -0
- data/lib/opal/rspec/rake_task.rb +63 -0
- data/lib/opal/rspec/version.rb +5 -0
- data/opal-rspec.gemspec +21 -0
- data/opal/opal-rspec.rb +1 -0
- data/opal/opal/rspec.rb +25 -0
- data/opal/opal/rspec/async.rb +289 -0
- data/opal/opal/rspec/browser_formatter.rb +188 -0
- data/opal/opal/rspec/fixes.rb +116 -0
- data/opal/opal/rspec/requires.rb +45 -0
- data/opal/opal/rspec/runner.rb +69 -0
- data/opal/opal/rspec/sprockets_runner.rb.erb +11 -0
- data/opal/opal/rspec/text_formatter.rb +74 -0
- data/spec/async_spec.rb +38 -0
- data/spec/example_spec.rb +163 -0
- data/spec/matchers_spec.rb +201 -0
- data/spec/mock_spec.rb +63 -0
- data/spec/named_subject_spec.rb +11 -0
- data/spec/should_syntax_spec.rb +17 -0
- data/vendor/spec_runner.js +50 -0
- data/vendor_lib/rspec-expectations.rb +1 -0
- data/vendor_lib/rspec.rb +3 -0
- data/vendor_lib/rspec/autorun.rb +2 -0
- data/vendor_lib/rspec/core.rb +203 -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.rb +54 -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/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.rb +146 -0
- data/vendor_lib/rspec/core/shared_example_group/collection.rb +27 -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/expectations.rb +75 -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.rb +1 -0
- data/vendor_lib/rspec/expectations/extensions/object.rb +29 -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/matchers.rb +633 -0
- data/vendor_lib/rspec/matchers/built_in.rb +39 -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/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/mocks.rb +100 -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/support.rb +6 -0
- data/vendor_lib/rspec/support/caller_filter.rb +56 -0
- data/vendor_lib/rspec/support/spec.rb +14 -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/version.rb +7 -0
- data/vendor_lib/rspec/support/warnings.rb +41 -0
- data/vendor_lib/rspec/version.rb +5 -0
- metadata +268 -0
@@ -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
|