opal-rspec-cj 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,95 @@
|
|
|
1
|
+
module RSpec
|
|
2
|
+
module Mocks
|
|
3
|
+
# @api private
|
|
4
|
+
class Space
|
|
5
|
+
attr_reader :proxies, :any_instance_recorders
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
@proxies = {}
|
|
9
|
+
@any_instance_recorders = {}
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def verify_all
|
|
13
|
+
proxies.each_value do |object|
|
|
14
|
+
object.verify
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
any_instance_recorders.each_value do |recorder|
|
|
18
|
+
recorder.verify
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def reset_all
|
|
23
|
+
ConstantMutator.reset_all
|
|
24
|
+
|
|
25
|
+
proxies.each_value do |object|
|
|
26
|
+
object.reset
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
proxies.clear
|
|
30
|
+
any_instance_recorders.clear
|
|
31
|
+
expectation_ordering.clear
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def expectation_ordering
|
|
35
|
+
@expectation_ordering ||= OrderGroup.new
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def any_instance_recorder_for(klass)
|
|
39
|
+
id = klass.__id__
|
|
40
|
+
any_instance_recorders.fetch(id) do
|
|
41
|
+
any_instance_recorders[id] = AnyInstance::Recorder.new(klass)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def remove_any_instance_recorder_for(klass)
|
|
46
|
+
any_instance_recorders.delete(klass.__id__)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def proxies_of(klass)
|
|
50
|
+
proxies.values.select { |proxy| klass === proxy.object }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def proxy_for(object)
|
|
54
|
+
id = id_for(object)
|
|
55
|
+
proxies.fetch(id) do
|
|
56
|
+
proxies[id] = case object
|
|
57
|
+
when NilClass then ProxyForNil.new(expectation_ordering)
|
|
58
|
+
when TestDouble then object.__build_mock_proxy(expectation_ordering)
|
|
59
|
+
else
|
|
60
|
+
if RSpec::Mocks.configuration.verify_partial_doubles?
|
|
61
|
+
VerifyingPartialMockProxy.new(object, expectation_ordering)
|
|
62
|
+
else
|
|
63
|
+
PartialMockProxy.new(object, expectation_ordering)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
alias ensure_registered proxy_for
|
|
70
|
+
|
|
71
|
+
def registered?(object)
|
|
72
|
+
proxies.has_key?(id_for object)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
if defined?(::BasicObject) && !::BasicObject.method_defined?(:__id__) # for 1.9.2
|
|
76
|
+
require 'securerandom'
|
|
77
|
+
|
|
78
|
+
def id_for(object)
|
|
79
|
+
id = object.__id__
|
|
80
|
+
|
|
81
|
+
return id if object.equal?(::ObjectSpace._id2ref(id))
|
|
82
|
+
# this suggests that object.__id__ is proxying through to some wrapped object
|
|
83
|
+
|
|
84
|
+
object.instance_exec do
|
|
85
|
+
@__id_for_rspec_mocks_space ||= ::SecureRandom.uuid
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
else
|
|
89
|
+
def id_for(object)
|
|
90
|
+
object.__id__
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
module RSpec
|
|
2
|
+
module Mocks
|
|
3
|
+
# @private
|
|
4
|
+
class StubChain
|
|
5
|
+
def self.stub_chain_on(object, *chain, &blk)
|
|
6
|
+
new(object, *chain, &blk).stub_chain
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
attr_reader :object, :chain, :block
|
|
10
|
+
|
|
11
|
+
def initialize(object, *chain, &blk)
|
|
12
|
+
@object = object
|
|
13
|
+
@chain, @block = format_chain(*chain, &blk)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def stub_chain
|
|
17
|
+
if chain.length > 1
|
|
18
|
+
if matching_stub = find_matching_stub
|
|
19
|
+
chain.shift
|
|
20
|
+
matching_stub.invoke(nil).stub_chain(*chain, &block)
|
|
21
|
+
else
|
|
22
|
+
next_in_chain = Mock.new
|
|
23
|
+
object.stub(chain.shift) { next_in_chain }
|
|
24
|
+
StubChain.stub_chain_on(next_in_chain, *chain, &block)
|
|
25
|
+
end
|
|
26
|
+
else
|
|
27
|
+
object.stub(chain.shift, &block)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def format_chain(*chain, &blk)
|
|
34
|
+
if Hash === chain.last
|
|
35
|
+
hash = chain.pop
|
|
36
|
+
hash.each do |k,v|
|
|
37
|
+
chain << k
|
|
38
|
+
blk = lambda { v }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
return chain.join('.').split('.'), blk
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def find_matching_stub
|
|
45
|
+
::RSpec::Mocks.proxy_for(object).
|
|
46
|
+
__send__(:find_matching_method_stub, chain.first.to_sym)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
@@ -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
|
+
|