rspec-mocks 2.99.4 → 3.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +14 -6
  2. checksums.yaml.gz.sig +2 -0
  3. data.tar.gz.sig +1 -0
  4. data/Changelog.md +89 -105
  5. data/License.txt +1 -0
  6. data/README.md +77 -57
  7. data/features/argument_matchers/explicit.feature +5 -5
  8. data/features/argument_matchers/general_matchers.feature +10 -10
  9. data/features/argument_matchers/type_matchers.feature +3 -3
  10. data/features/message_expectations/allow_any_instance_of.feature +1 -1
  11. data/features/message_expectations/any_instance.feature +27 -5
  12. data/features/message_expectations/call_original.feature +2 -2
  13. data/features/message_expectations/expect_message_using_expect.feature +2 -2
  14. data/features/message_expectations/expect_message_using_should_receive.feature +2 -2
  15. data/features/message_expectations/receive_counts.feature +7 -7
  16. data/features/message_expectations/warn_when_expectation_is_set_on_nil.feature +3 -3
  17. data/features/method_stubs/README.md +3 -0
  18. data/features/method_stubs/any_instance.feature +11 -11
  19. data/features/method_stubs/as_null_object.feature +4 -4
  20. data/features/method_stubs/simple_return_value_with_stub.feature +7 -7
  21. data/features/method_stubs/stub_chain.feature +3 -3
  22. data/features/method_stubs/stub_implementation.feature +2 -2
  23. data/features/method_stubs/to_ary.feature +2 -2
  24. data/features/mutating_constants/hiding_defined_constant.feature +2 -2
  25. data/features/mutating_constants/stub_defined_constant.feature +5 -5
  26. data/features/mutating_constants/stub_undefined_constant.feature +6 -6
  27. data/features/outside_rspec/configuration.feature +0 -2
  28. data/features/outside_rspec/standalone.feature +1 -1
  29. data/features/spies/spy_partial_mock_method.feature +2 -2
  30. data/features/spies/spy_pure_mock_method.feature +5 -5
  31. data/features/spies/spy_unstubbed_method.feature +1 -1
  32. data/features/support/env.rb +10 -1
  33. data/features/test_frameworks/test_unit.feature +1 -1
  34. data/features/verifying_doubles/class_doubles.feature +88 -0
  35. data/features/verifying_doubles/dynamic_classes.feature +72 -0
  36. data/features/verifying_doubles/introduction.feature +85 -0
  37. data/features/verifying_doubles/object_doubles.feature +65 -0
  38. data/features/verifying_doubles/partial_doubles.feature +34 -0
  39. data/lib/rspec/mocks.rb +8 -34
  40. data/lib/rspec/mocks/any_instance/chain.rb +4 -34
  41. data/lib/rspec/mocks/any_instance/expectation_chain.rb +14 -4
  42. data/lib/rspec/mocks/any_instance/message_chains.rb +27 -12
  43. data/lib/rspec/mocks/any_instance/recorder.rb +23 -31
  44. data/lib/rspec/mocks/any_instance/stub_chain.rb +9 -4
  45. data/lib/rspec/mocks/argument_list_matcher.rb +8 -1
  46. data/lib/rspec/mocks/argument_matchers.rb +26 -12
  47. data/lib/rspec/mocks/arity_calculator.rb +66 -0
  48. data/lib/rspec/mocks/configuration.rb +42 -14
  49. data/lib/rspec/mocks/error_generator.rb +34 -10
  50. data/lib/rspec/mocks/example_methods.rb +64 -19
  51. data/lib/rspec/mocks/extensions/marshal.rb +0 -15
  52. data/lib/rspec/mocks/framework.rb +4 -4
  53. data/lib/rspec/mocks/instance_method_stasher.rb +80 -62
  54. data/lib/rspec/mocks/matchers/have_received.rb +18 -14
  55. data/lib/rspec/mocks/matchers/receive.rb +29 -7
  56. data/lib/rspec/mocks/matchers/receive_messages.rb +72 -0
  57. data/lib/rspec/mocks/message_expectation.rb +95 -148
  58. data/lib/rspec/mocks/method_double.rb +77 -139
  59. data/lib/rspec/mocks/method_reference.rb +95 -0
  60. data/lib/rspec/mocks/mock.rb +1 -1
  61. data/lib/rspec/mocks/mutate_const.rb +12 -9
  62. data/lib/rspec/mocks/object_reference.rb +90 -0
  63. data/lib/rspec/mocks/order_group.rb +49 -7
  64. data/lib/rspec/mocks/proxy.rb +72 -33
  65. data/lib/rspec/mocks/proxy_for_nil.rb +2 -2
  66. data/lib/rspec/mocks/space.rb +13 -18
  67. data/lib/rspec/mocks/stub_chain.rb +2 -2
  68. data/lib/rspec/mocks/syntax.rb +61 -36
  69. data/lib/rspec/mocks/targets.rb +40 -19
  70. data/lib/rspec/mocks/test_double.rb +12 -56
  71. data/lib/rspec/mocks/verifying_double.rb +77 -0
  72. data/lib/rspec/mocks/verifying_message_expecation.rb +60 -0
  73. data/lib/rspec/mocks/verifying_proxy.rb +151 -0
  74. data/lib/rspec/mocks/version.rb +1 -1
  75. data/spec/rspec/mocks/and_call_original_spec.rb +34 -30
  76. data/spec/rspec/mocks/and_yield_spec.rb +2 -2
  77. data/spec/rspec/mocks/any_instance/message_chains_spec.rb +1 -1
  78. data/spec/rspec/mocks/any_instance_spec.rb +53 -260
  79. data/spec/rspec/mocks/argument_expectation_spec.rb +4 -4
  80. data/spec/rspec/mocks/arity_calculator_spec.rb +95 -0
  81. data/spec/rspec/mocks/array_including_matcher_spec.rb +41 -0
  82. data/spec/rspec/mocks/at_least_spec.rb +4 -32
  83. data/spec/rspec/mocks/block_return_value_spec.rb +4 -135
  84. data/spec/rspec/mocks/combining_implementation_instructions_spec.rb +10 -11
  85. data/spec/rspec/mocks/configuration_spec.rb +79 -0
  86. data/spec/rspec/mocks/double_spec.rb +10 -78
  87. data/spec/rspec/mocks/extensions/marshal_spec.rb +0 -8
  88. data/spec/rspec/mocks/failing_argument_matchers_spec.rb +49 -4
  89. data/spec/rspec/mocks/instance_method_stasher_spec.rb +20 -3
  90. data/spec/rspec/mocks/matchers/have_received_spec.rb +74 -0
  91. data/spec/rspec/mocks/matchers/receive_messages_spec.rb +140 -0
  92. data/spec/rspec/mocks/matchers/receive_spec.rb +82 -42
  93. data/spec/rspec/mocks/methods_spec.rb +1 -1
  94. data/spec/rspec/mocks/{bug_report_830_spec.rb → mock_expectation_error_spec.rb} +4 -3
  95. data/spec/rspec/mocks/mock_ordering_spec.rb +11 -0
  96. data/spec/rspec/mocks/mock_space_spec.rb +10 -1
  97. data/spec/rspec/mocks/mock_spec.rb +26 -82
  98. data/spec/rspec/mocks/multiple_return_value_spec.rb +1 -1
  99. data/spec/rspec/mocks/mutate_const_spec.rb +18 -5
  100. data/spec/rspec/mocks/null_object_mock_spec.rb +6 -4
  101. data/spec/rspec/mocks/options_hash_spec.rb +3 -3
  102. data/spec/rspec/mocks/order_group_spec.rb +27 -0
  103. data/spec/rspec/mocks/partial_mock_spec.rb +101 -1
  104. data/spec/rspec/mocks/passing_argument_matchers_spec.rb +3 -20
  105. data/spec/rspec/mocks/record_messages_spec.rb +4 -4
  106. data/spec/rspec/mocks/serialization_spec.rb +4 -6
  107. data/spec/rspec/mocks/space_spec.rb +3 -3
  108. data/spec/rspec/mocks/stub_chain_spec.rb +0 -12
  109. data/spec/rspec/mocks/stub_spec.rb +23 -44
  110. data/spec/rspec/mocks/test_double_spec.rb +3 -22
  111. data/spec/rspec/mocks/verifying_double_spec.rb +327 -0
  112. data/spec/rspec/mocks/verifying_message_expecation_spec.rb +68 -0
  113. data/spec/rspec/mocks_spec.rb +16 -39
  114. data/spec/spec_helper.rb +29 -18
  115. metadata +131 -86
  116. metadata.gz.sig +1 -0
  117. data/features/message_expectations/expect_any_instance_of.feature +0 -27
  118. data/lib/rspec/mocks/caller_filter.rb +0 -60
  119. data/lib/rspec/mocks/deprecation.rb +0 -26
  120. data/lib/rspec/mocks/extensions/instance_exec.rb +0 -34
  121. data/lib/rspec/mocks/extensions/proc.rb +0 -63
  122. data/lib/spec/mocks.rb +0 -4
  123. data/spec/rspec/mocks/and_return_spec.rb +0 -17
  124. data/spec/rspec/mocks/any_number_of_times_spec.rb +0 -36
  125. data/spec/rspec/mocks/before_all_spec.rb +0 -74
  126. data/spec/rspec/mocks/bug_report_10260_spec.rb +0 -8
  127. data/spec/rspec/mocks/bug_report_10263_spec.rb +0 -27
  128. data/spec/rspec/mocks/bug_report_11545_spec.rb +0 -32
  129. data/spec/rspec/mocks/bug_report_496_spec.rb +0 -17
  130. data/spec/rspec/mocks/bug_report_600_spec.rb +0 -22
  131. data/spec/rspec/mocks/bug_report_7611_spec.rb +0 -16
  132. data/spec/rspec/mocks/bug_report_8165_spec.rb +0 -31
  133. data/spec/rspec/mocks/bug_report_957_spec.rb +0 -22
@@ -0,0 +1 @@
1
+ ���
@@ -1,27 +0,0 @@
1
- Feature: expect a message on any instance of a class
2
-
3
- Use `expect_any_instance_of(Class).to receive` to set an expectation that one
4
- (and only one) instance of a class receives a message before the example is
5
- completed. The spec will fail if no instance receives a message.
6
-
7
- Scenario: expect a message on any instance of a class
8
- Given a file named "example_spec.rb" with:
9
- """ruby
10
- describe "expect_any_instance_of" do
11
- before do
12
- expect_any_instance_of(Object).to receive(:foo).and_return(:return_value)
13
- end
14
-
15
- it "verifies that one instance of the class receives the message" do
16
- o = Object.new
17
- expect(o.foo).to eq(:return_value)
18
- end
19
-
20
- it "fails unless an instance receives that message" do
21
- o = Object.new
22
- end
23
- end
24
- """
25
- When I run `rspec example_spec.rb`
26
- Then the output should contain "2 examples, 1 failure"
27
- And the output should contain "1) expect_any_instance_of fails unless an instance receives that message"
@@ -1,60 +0,0 @@
1
- module RSpec
2
- # Consistent implementation for "cleaning" the caller method to strip out
3
- # non-rspec lines. This enables errors to be reported at the call site in
4
- # the code using the library, which is far more useful than the particular
5
- # internal method that raised an error.
6
-
7
- unless defined?(CallerFilter)
8
-
9
- class CallerFilter
10
-
11
- RSPEC_LIBS = %w[
12
- core
13
- mocks
14
- expectations
15
- matchers
16
- rails
17
- ]
18
-
19
- ADDITIONAL_TOP_LEVEL_FILES = %w[ autorun ]
20
-
21
- LIB_REGEX = %r{/lib/rspec/(#{(RSPEC_LIBS + ADDITIONAL_TOP_LEVEL_FILES).join('|')})(\.rb|/)}
22
-
23
- if RUBY_VERSION >= '2.0.0'
24
- def self.first_non_rspec_line
25
- # `caller` is an expensive method that scales linearly with the size of
26
- # the stack. The performance hit for fetching it in chunks is small,
27
- # and since the target line is probably near the top of the stack, the
28
- # overall improvement of a chunked search like this is significant.
29
- #
30
- # See benchmarks/caller.rb for measurements.
31
-
32
- # Initial value here is mostly arbitrary, but is chosen to give good
33
- # performance on the common case of creating a double.
34
- increment = 5
35
- i = 1
36
- line = nil
37
-
38
- while !line
39
- stack = caller(i, increment)
40
- return nil unless stack
41
-
42
- line = stack.find { |l| l !~ LIB_REGEX }
43
-
44
- i += increment
45
- increment *= 2 # The choice of two here is arbitrary.
46
- end
47
-
48
- line
49
- end
50
- else
51
- # Earlier rubies do not support the two argument form of `caller`. This
52
- # fallback is logically the same, but slower.
53
- def self.first_non_rspec_line
54
- caller.find { |line| line !~ LIB_REGEX }
55
- end
56
- end
57
- end
58
-
59
- end
60
- end
@@ -1,26 +0,0 @@
1
- module RSpec
2
- module Mocks
3
- module Deprecation
4
- # @private
5
- #
6
- # Used internally to print deprecation warnings
7
- def deprecate(deprecated, options={})
8
- message = "DEPRECATION: #{deprecated} is deprecated."
9
- message << " Use #{options[:replacement]} instead." if options[:replacement]
10
- message << " Called from #{CallerFilter.first_non_rspec_line}."
11
- warn message
12
- end
13
-
14
- # @private
15
- #
16
- # Used internally to print deprecation warnings
17
- def warn_deprecation(warning)
18
- message = "\nDEPRECATION: #{warning}\n"
19
- warn message
20
- end
21
- end
22
- end
23
-
24
- extend(Mocks::Deprecation) unless respond_to?(:deprecate)
25
- end
26
-
@@ -1,34 +0,0 @@
1
- module RSpec
2
- module Mocks
3
- # @private
4
- module InstanceExec
5
- unless respond_to?(:instance_exec)
6
- # @private
7
- #
8
- # based on Bounded Spec InstanceExec (Mauricio Fernandez)
9
- # http://eigenclass.org/hiki/bounded+space+instance_exec
10
- # - uses singleton_class of matcher instead of global
11
- # InstanceExecHelper module
12
- # - this keeps it scoped to this class only, which is the
13
- # only place we need it
14
- # - only necessary for ruby 1.8.6
15
- def instance_exec(*args, &block)
16
- singleton_class = (class << self; self; end)
17
- begin
18
- orig_critical, Thread.critical = Thread.critical, true
19
- n = 0
20
- n += 1 while respond_to?(method_name="__instance_exec#{n}")
21
- singleton_class.module_eval{ define_method(method_name, &block) }
22
- ensure
23
- Thread.critical = orig_critical
24
- end
25
- begin
26
- return send(method_name, *args)
27
- ensure
28
- singleton_class.module_eval{ remove_method(method_name) } rescue nil
29
- end
30
- end
31
- end
32
- end
33
- end
34
- end
@@ -1,63 +0,0 @@
1
- unless Proc.method_defined? :lambda?
2
- module Backports
3
- def self.alias_method_chain(mod, target, feature)
4
- mod.class_eval do
5
- # Strip out punctuation on predicates or bang methods since
6
- # e.g. target?_without_feature is not a valid method name.
7
- aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
8
- yield(aliased_target, punctuation) if block_given?
9
-
10
- with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
11
-
12
- alias_method without_method, target
13
- alias_method target, with_method
14
-
15
- case
16
- when public_method_defined?(without_method)
17
- public target
18
- when protected_method_defined?(without_method)
19
- protected target
20
- when private_method_defined?(without_method)
21
- private target
22
- end
23
- end
24
- end
25
- end
26
-
27
- class Proc
28
- # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Proc.html]
29
- def lambda?
30
- !!__is_lambda__
31
- end
32
-
33
- attr_accessor :__is_lambda__
34
- private :__is_lambda__
35
- private :__is_lambda__=
36
- end
37
-
38
- class Method
39
- def to_proc_with_lambda_tracking
40
- proc = to_proc_without_lambda_tracking
41
- proc.send :__is_lambda__=, true
42
- proc
43
- end
44
- Backports.alias_method_chain self, :to_proc, :lambda_tracking
45
- end
46
-
47
- module Kernel
48
- def lambda_with_lambda_tracking(&block)
49
- l = lambda_without_lambda_tracking(&block)
50
- l.send :__is_lambda__=, true unless block.send(:__is_lambda__) == false
51
- l
52
- end
53
-
54
- def proc_with_lambda_tracking(&block)
55
- l = proc_without_lambda_tracking(&block)
56
- l.send :__is_lambda__=, block.send(:__is_lambda__) == true
57
- l
58
- end
59
-
60
- Backports.alias_method_chain self, :lambda, :lambda_tracking
61
- Backports.alias_method_chain self, :proc, :lambda_tracking
62
- end
63
- end
@@ -1,4 +0,0 @@
1
- # For backward compatibility with rspec-1
2
- require 'rspec/mocks'
3
-
4
- RSpec.deprecate "require 'spec/mocks'", :replacement => "require 'rspec/mocks'"
@@ -1,17 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec
4
- module Mocks
5
- describe 'and_return' do
6
- let(:obj) { double('obj') }
7
-
8
- context 'when no argument is passed' do
9
- it 'warns of deprection' do
10
- expect_warn_deprecation_with_call_site(__FILE__, __LINE__ + 1, '`and_return` without arguments')
11
- obj.stub(:foo).and_return
12
- expect(obj.foo).to be_nil
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,36 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "AnyNumberOfTimes" do
4
- before(:each) do
5
- @mock = RSpec::Mocks::Double.new("test mock")
6
- allow(RSpec).to receive(:deprecate)
7
- end
8
-
9
- it "is deprecated" do
10
- expect(RSpec).to receive(:deprecate).with("any_number_of_times", :replacement => "stub")
11
- expect(@mock).to receive(:random_call).any_number_of_times
12
- end
13
-
14
- it "passes if any number of times method is called many times" do
15
- @mock.should_receive(:random_call).any_number_of_times
16
- (1..10).each do
17
- @mock.random_call
18
- end
19
- end
20
-
21
- it "passes if any number of times method is called once" do
22
- @mock.should_receive(:random_call).any_number_of_times
23
- @mock.random_call
24
- end
25
-
26
- it "passes if any number of times method is not called" do
27
- @mock.should_receive(:random_call).any_number_of_times
28
- end
29
-
30
- it "returns the mocked value when called after a similar stub" do
31
- @mock.stub(:message).and_return :stub_value
32
- @mock.should_receive(:message).any_number_of_times.and_return(:mock_value)
33
- expect(@mock.message).to eq :mock_value
34
- expect(@mock.message).to eq :mock_value
35
- end
36
- end
@@ -1,74 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "using rspec-mocks constructs in before(:all)" do
4
- deprecations = []
5
-
6
- def in_rspec_singleton_class(&block)
7
- klass = class << ::RSpec; self; end
8
- klass.class_eval(&block)
9
- end
10
-
11
- before(:all) do
12
- in_rspec_singleton_class do
13
- alias old_deprecate deprecate
14
- undef deprecate
15
- define_method(:deprecate) { |*args| deprecations << args.first }
16
- end
17
- end
18
-
19
- after(:all) do
20
- in_rspec_singleton_class do
21
- undef deprecate
22
- alias deprecate old_deprecate
23
- undef old_deprecate
24
- end
25
- end
26
-
27
- describe "a method stub" do
28
- before(:all) do
29
- deprecations.clear
30
- Object.stub(:foo) { 13 }
31
- end
32
-
33
- it 'works in examples and prints a deprecation' do
34
- expect(Object.foo).to eq(13)
35
- expect(deprecations).to include(match(/outside the per-test lifecycle/))
36
- end
37
- end
38
-
39
- describe "an any_instance stub" do
40
- before(:all) do
41
- deprecations.clear
42
- Object.any_instance.stub(:foo => 13)
43
- end
44
-
45
- it 'works in examples and prints a deprecation' do
46
- expect(Object.new.foo).to eq(13)
47
- expect(deprecations).to include(match(/outside the per-test lifecycle/))
48
- end
49
- end
50
-
51
- describe "constant stubbing" do
52
- before(:all) do
53
- deprecations.clear
54
- RSpec::Mocks::ConstantMutator.stub("Foo23", 23)
55
- end
56
-
57
- it 'works in examples and prints a deprecation' do
58
- expect(Foo23).to eq(23)
59
- expect(deprecations).to include(match(/outside the per-test lifecycle/))
60
- end
61
- end
62
-
63
- describe "constant hiding" do
64
- before(:all) do
65
- deprecations.clear
66
- RSpec::Mocks::ConstantMutator.hide("SomeClass")
67
- end
68
-
69
- it 'works in examples and prints a deprecation' do
70
- expect(deprecations).to include(match(/outside the per-test lifecycle/))
71
- end
72
- end
73
- end
74
-
@@ -1,8 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "An RSpec Mock" do
4
- it "hides internals in its inspect representation" do
5
- m = double('cup')
6
- expect(m.inspect).to match(/#<RSpec::Mocks::Double:0x[a-f0-9.]+ @name="cup">/)
7
- end
8
- end
@@ -1,27 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Double" do
4
- let(:test_double) { double }
5
-
6
- specify "when one example has an expectation inside the block passed to should_receive" do
7
- test_double.should_receive(:msg) do |arg|
8
- expect(arg).to be true #this call exposes the problem
9
- end
10
- begin
11
- test_double.msg(false)
12
- rescue Exception
13
- end
14
- end
15
-
16
- specify "then the next example should behave as expected instead of saying" do
17
- test_double.should_receive(:foobar)
18
- test_double.foobar
19
- verify test_double
20
- begin
21
- test_double.foobar
22
- rescue Exception => e
23
- expect(e.message).to eq "Double received unexpected message :foobar with (no args)"
24
- end
25
- end
26
- end
27
-
@@ -1,32 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class LiarLiarPantsOnFire
4
- def respond_to?(message, incl_private=false)
5
- true
6
- end
7
-
8
- def self.respond_to?(message, incl_private=false)
9
- true
10
- end
11
- end
12
-
13
- describe 'should_receive' do
14
- before(:each) do
15
- @liar = LiarLiarPantsOnFire.new
16
- end
17
-
18
- it "works when object lies about responding to a method" do
19
- @liar.should_receive(:something)
20
- @liar.something
21
- end
22
-
23
- it 'works when class lies about responding to a method' do
24
- LiarLiarPantsOnFire.should_receive(:something)
25
- LiarLiarPantsOnFire.something
26
- end
27
-
28
- it 'cleans up after itself' do
29
- expect((class << LiarLiarPantsOnFire; self; end).instance_methods).not_to include("something")
30
- end
31
- end
32
-
@@ -1,17 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module BugReport496
4
- describe "a message expectation on a base class object" do
5
- class BaseClass
6
- end
7
-
8
- class SubClass < BaseClass
9
- end
10
-
11
- it "is received" do
12
- BaseClass.should_receive(:new).once
13
- SubClass.new
14
- end
15
- end
16
- end
17
-