rr 1.1.1 → 1.1.2.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. data/Appraisals +46 -0
  3. data/CHANGES.md +21 -1
  4. data/Gemfile +19 -0
  5. data/README.md +91 -106
  6. data/Rakefile +55 -0
  7. data/gemfiles/ruby_18_rspec_1.gemfile +14 -0
  8. data/gemfiles/ruby_18_rspec_1.gemfile.lock +38 -0
  9. data/gemfiles/ruby_18_rspec_1_rails_2.gemfile +18 -0
  10. data/gemfiles/ruby_18_rspec_1_rails_2.gemfile.lock +64 -0
  11. data/gemfiles/ruby_19_rspec_2.gemfile +14 -0
  12. data/gemfiles/ruby_19_rspec_2.gemfile.lock +49 -0
  13. data/gemfiles/ruby_19_rspec_2_rails_3.gemfile +15 -0
  14. data/gemfiles/ruby_19_rspec_2_rails_3.gemfile.lock +123 -0
  15. data/gemfiles/ruby_19_rspec_2_rails_4.gemfile +15 -0
  16. data/gemfiles/ruby_19_rspec_2_rails_4.gemfile.lock +119 -0
  17. data/lib/rr/adapters.rb +4 -14
  18. data/lib/rr/integrations.rb +13 -12
  19. data/lib/rr/integrations/decorator.rb +4 -1
  20. data/lib/rr/integrations/rspec_2.rb +6 -1
  21. data/lib/rr/integrations/test_unit_1.rb +2 -2
  22. data/lib/rr/integrations/test_unit_2.rb +3 -1
  23. data/lib/rr/version.rb +1 -2
  24. data/lib/rr/without_autohook.rb +1 -1
  25. data/rr.gemspec +6 -2
  26. data/spec/custom_formatter_for_rspec.rb +18 -0
  27. data/spec/custom_formatter_for_rspec_2.rb +40 -0
  28. data/spec/defines_spec_suite_tasks.rb +45 -0
  29. data/spec/fixtures/rubygems_patch_for_187.rb +598 -0
  30. data/spec/global_helper.rb +33 -0
  31. data/spec/spec.opts +3 -0
  32. data/spec/spec_suite_configuration.rb +132 -0
  33. data/spec/spec_suite_runner.rb +47 -0
  34. data/spec/suites.yml +24 -0
  35. data/spec/suites/rspec_1/helper.rb +24 -0
  36. data/spec/suites/rspec_1/integration/rspec_1_spec.rb +93 -0
  37. data/spec/suites/rspec_1/integration/test_unit_1_spec.rb +102 -0
  38. data/spec/suites/rspec_1/integration/test_unit_2_spec.rb +109 -0
  39. data/spec/suites/rspec_1/spec_helper.rb +3 -0
  40. data/spec/suites/rspec_1_rails_2/integration/astc_rails_2_spec.rb +141 -0
  41. data/spec/suites/rspec_1_rails_2/integration/rspec_1_rails_2_spec.rb +132 -0
  42. data/spec/suites/rspec_1_rails_2/integration/test_unit_1_rails_2_spec.rb +141 -0
  43. data/spec/suites/rspec_1_rails_2/integration/test_unit_2_rails_2_spec.rb +148 -0
  44. data/spec/suites/rspec_1_rails_2/spec_helper.rb +3 -0
  45. data/spec/suites/rspec_2/functional/any_instance_of_spec.rb +47 -0
  46. data/spec/suites/rspec_2/functional/dont_allow_spec.rb +12 -0
  47. data/spec/suites/rspec_2/functional/dsl_spec.rb +13 -0
  48. data/spec/suites/rspec_2/functional/instance_of_spec.rb +14 -0
  49. data/spec/suites/rspec_2/functional/mock_spec.rb +241 -0
  50. data/spec/suites/rspec_2/functional/proxy_spec.rb +136 -0
  51. data/spec/suites/rspec_2/functional/spy_spec.rb +41 -0
  52. data/spec/suites/rspec_2/functional/strong_spec.rb +79 -0
  53. data/spec/suites/rspec_2/functional/stub_spec.rb +190 -0
  54. data/spec/suites/rspec_2/functional/wildcard_matchers_spec.rb +128 -0
  55. data/spec/suites/rspec_2/helper.rb +28 -0
  56. data/spec/suites/rspec_2/integration/minitest_4_spec.rb +109 -0
  57. data/spec/suites/rspec_2/integration/minitest_spec.rb +109 -0
  58. data/spec/suites/rspec_2/integration/rspec_2_spec.rb +133 -0
  59. data/spec/suites/rspec_2/integration/test_unit_200_spec.rb +102 -0
  60. data/spec/suites/rspec_2/integration/test_unit_2_spec.rb +109 -0
  61. data/spec/suites/rspec_2/spec_helper.rb +3 -0
  62. data/spec/suites/rspec_2/support/matchers/wildcard_matcher_matchers.rb +32 -0
  63. data/spec/suites/rspec_2/support/shared_examples/space.rb +13 -0
  64. data/spec/suites/rspec_2/support/shared_examples/times_called_expectation.rb +9 -0
  65. data/spec/suites/rspec_2/unit/adapters/rr_methods/double_creators_spec.rb +135 -0
  66. data/spec/suites/rspec_2/unit/adapters/rr_methods/space_spec.rb +101 -0
  67. data/spec/suites/rspec_2/unit/adapters/rr_methods/wildcard_matchers_spec.rb +69 -0
  68. data/spec/suites/rspec_2/unit/core_ext/array_spec.rb +39 -0
  69. data/spec/suites/rspec_2/unit/core_ext/enumerable_spec.rb +81 -0
  70. data/spec/suites/rspec_2/unit/core_ext/hash_spec.rb +55 -0
  71. data/spec/suites/rspec_2/unit/core_ext/range_spec.rb +41 -0
  72. data/spec/suites/rspec_2/unit/core_ext/regexp_spec.rb +41 -0
  73. data/spec/suites/rspec_2/unit/double_definitions/child_double_definition_create_spec.rb +114 -0
  74. data/spec/suites/rspec_2/unit/double_definitions/double_definition_create_blank_slate_spec.rb +93 -0
  75. data/spec/suites/rspec_2/unit/double_definitions/double_definition_create_spec.rb +446 -0
  76. data/spec/suites/rspec_2/unit/errors/rr_error_spec.rb +67 -0
  77. data/spec/suites/rspec_2/unit/expectations/any_argument_expectation_spec.rb +48 -0
  78. data/spec/suites/rspec_2/unit/expectations/anything_argument_equality_expectation_spec.rb +14 -0
  79. data/spec/suites/rspec_2/unit/expectations/argument_equality_expectation_spec.rb +135 -0
  80. data/spec/suites/rspec_2/unit/expectations/boolean_argument_equality_expectation_spec.rb +30 -0
  81. data/spec/suites/rspec_2/unit/expectations/hash_including_argument_equality_expectation_spec.rb +82 -0
  82. data/spec/suites/rspec_2/unit/expectations/satisfy_argument_equality_expectation_spec.rb +61 -0
  83. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/any_times_matcher_spec.rb +22 -0
  84. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/at_least_matcher_spec.rb +37 -0
  85. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/at_most_matcher_spec.rb +43 -0
  86. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/integer_matcher_spec.rb +58 -0
  87. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/proc_matcher_spec.rb +35 -0
  88. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/range_matcher_spec.rb +39 -0
  89. data/spec/suites/rspec_2/unit/hash_with_object_id_key_spec.rb +88 -0
  90. data/spec/suites/rspec_2/unit/injections/double_injection/double_injection_spec.rb +545 -0
  91. data/spec/suites/rspec_2/unit/injections/double_injection/double_injection_verify_spec.rb +32 -0
  92. data/spec/suites/rspec_2/unit/integrations/rspec/invocation_matcher_spec.rb +297 -0
  93. data/spec/suites/rspec_2/unit/integrations/rspec_spec.rb +85 -0
  94. data/spec/suites/rspec_2/unit/proc_from_block_spec.rb +14 -0
  95. data/spec/suites/rspec_2/unit/rr_spec.rb +28 -0
  96. data/spec/suites/rspec_2/unit/space_spec.rb +595 -0
  97. data/spec/suites/rspec_2/unit/spy_verification_spec.rb +133 -0
  98. data/spec/suites/rspec_2/unit/times_called_matchers/any_times_matcher_spec.rb +46 -0
  99. data/spec/suites/rspec_2/unit/times_called_matchers/at_least_matcher_spec.rb +54 -0
  100. data/spec/suites/rspec_2/unit/times_called_matchers/at_most_matcher_spec.rb +69 -0
  101. data/spec/suites/rspec_2/unit/times_called_matchers/integer_matcher_spec.rb +69 -0
  102. data/spec/suites/rspec_2/unit/times_called_matchers/proc_matcher_spec.rb +54 -0
  103. data/spec/suites/rspec_2/unit/times_called_matchers/range_matcher_spec.rb +75 -0
  104. data/spec/suites/rspec_2/unit/times_called_matchers/times_called_matcher_spec.rb +117 -0
  105. data/spec/suites/rspec_2/unit/wildcard_matchers/anything_spec.rb +33 -0
  106. data/spec/suites/rspec_2/unit/wildcard_matchers/boolean_spec.rb +45 -0
  107. data/spec/suites/rspec_2/unit/wildcard_matchers/duck_type_spec.rb +64 -0
  108. data/spec/suites/rspec_2/unit/wildcard_matchers/hash_including_spec.rb +64 -0
  109. data/spec/suites/rspec_2/unit/wildcard_matchers/is_a_spec.rb +55 -0
  110. data/spec/suites/rspec_2/unit/wildcard_matchers/numeric_spec.rb +46 -0
  111. data/spec/suites/rspec_2/unit/wildcard_matchers/satisfy_spec.rb +57 -0
  112. data/spec/suites/rspec_2_rails_3/integration/astc_rails_3_spec.rb +141 -0
  113. data/spec/suites/rspec_2_rails_3/integration/minitest_4_rails_3_spec.rb +148 -0
  114. data/spec/suites/rspec_2_rails_3/integration/rspec_2_rails_3_spec.rb +172 -0
  115. data/spec/suites/rspec_2_rails_3/integration/test_unit_200_rails_3_spec.rb +141 -0
  116. data/spec/suites/rspec_2_rails_3/integration/test_unit_2_rails_3_spec.rb +148 -0
  117. data/spec/suites/rspec_2_rails_3/spec_helper.rb +3 -0
  118. data/spec/suites/rspec_2_rails_4/integration/astc_rails_4_spec.rb +142 -0
  119. data/spec/suites/rspec_2_rails_4/integration/minitest_4_rails_4_spec.rb +149 -0
  120. data/spec/suites/rspec_2_rails_4/integration/rspec_2_rails_4_spec.rb +173 -0
  121. data/spec/suites/rspec_2_rails_4/integration/test_unit_200_rails_4_spec.rb +142 -0
  122. data/spec/suites/rspec_2_rails_4/integration/test_unit_2_rails_4_spec.rb +149 -0
  123. data/spec/suites/rspec_2_rails_4/spec_helper.rb +3 -0
  124. data/spec/support/adapter.rb +22 -0
  125. data/spec/support/adapter_tests/base.rb +45 -0
  126. data/spec/support/adapter_tests/minitest.rb +7 -0
  127. data/spec/support/adapter_tests/rspec.rb +66 -0
  128. data/spec/support/adapter_tests/test_unit.rb +47 -0
  129. data/spec/support/command_runner.rb +105 -0
  130. data/spec/support/generator.rb +56 -0
  131. data/spec/support/integration_tests/base.rb +64 -0
  132. data/spec/support/integration_tests/rails.rb +60 -0
  133. data/spec/support/integration_tests/rails_minitest.rb +13 -0
  134. data/spec/support/integration_tests/rails_rspec.rb +13 -0
  135. data/spec/support/integration_tests/rails_test_unit.rb +13 -0
  136. data/spec/support/integration_tests/rails_test_unit_like.rb +13 -0
  137. data/spec/support/integration_tests/ruby.rb +7 -0
  138. data/spec/support/integration_tests/ruby_minitest.rb +13 -0
  139. data/spec/support/integration_tests/ruby_rspec.rb +13 -0
  140. data/spec/support/integration_tests/ruby_test_unit.rb +13 -0
  141. data/spec/support/matchers/be_a_subset_of_matcher.rb +24 -0
  142. data/spec/support/project/cucumber.rb +50 -0
  143. data/spec/support/project/generator.rb +352 -0
  144. data/spec/support/project/minitest.rb +39 -0
  145. data/spec/support/project/rails.rb +199 -0
  146. data/spec/support/project/rails_minitest.rb +17 -0
  147. data/spec/support/project/rails_rspec.rb +50 -0
  148. data/spec/support/project/rails_test_unit.rb +17 -0
  149. data/spec/support/project/rails_test_unit_like.rb +17 -0
  150. data/spec/support/project/rspec.rb +69 -0
  151. data/spec/support/project/ruby.rb +34 -0
  152. data/spec/support/project/ruby_minitest.rb +11 -0
  153. data/spec/support/project/ruby_rspec.rb +29 -0
  154. data/spec/support/project/ruby_test_unit.rb +11 -0
  155. data/spec/support/project/ruby_test_unit_like.rb +21 -0
  156. data/spec/support/project/test_unit.rb +29 -0
  157. data/spec/support/project/test_unit_like.rb +7 -0
  158. data/spec/support/project/tests_runner.rb +22 -0
  159. data/spec/support/test.sqlite3 +0 -0
  160. data/spec/support/test_case/generator.rb +53 -0
  161. data/spec/support/test_case/minitest.rb +13 -0
  162. data/spec/support/test_case/rspec.rb +19 -0
  163. data/spec/support/test_case/test_unit.rb +21 -0
  164. data/spec/support/test_file/generator.rb +120 -0
  165. data/spec/support/test_file/minitest.rb +19 -0
  166. data/spec/support/test_file/rails_minitest.rb +7 -0
  167. data/spec/support/test_file/rails_rspec.rb +12 -0
  168. data/spec/support/test_file/rails_test_unit.rb +25 -0
  169. data/spec/support/test_file/rspec.rb +33 -0
  170. data/spec/support/test_file/test_unit.rb +36 -0
  171. data/spec/support/test_helper/generator.rb +27 -0
  172. data/spec/support/test_helper/minitest.rb +7 -0
  173. data/spec/support/test_helper/rails.rb +31 -0
  174. data/spec/support/test_helper/rails_minitest.rb +7 -0
  175. data/spec/support/test_helper/rails_rspec.rb +25 -0
  176. data/spec/support/test_helper/rails_test_unit.rb +23 -0
  177. data/spec/support/test_helper/rspec.rb +7 -0
  178. data/spec/support/test_helper/ruby.rb +31 -0
  179. data/spec/support/test_helper/test_unit.rb +7 -0
  180. metadata +172 -6
  181. data/VERSION +0 -1
@@ -0,0 +1,58 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../../../spec_helper")
2
+
3
+ module RR
4
+ module Expectations
5
+ describe TimesCalledExpectation do
6
+ context "when using an IntegerMatcher" do
7
+ include_examples "RR::Expectations::TimesCalledExpectation"
8
+
9
+ before do
10
+ stub(subject).foobar.times(2)
11
+ end
12
+
13
+ describe "verify" do
14
+ it "passes after attempt! called 2 times" do
15
+ subject.foobar
16
+ subject.foobar
17
+ RR.verify
18
+ end
19
+
20
+ it "fails after attempt! called 1 time" do
21
+ subject.foobar
22
+ expect { RR.verify }.to raise_error(
23
+ RR::Errors::TimesCalledError,
24
+ "foobar()\nCalled 1 time.\nExpected 2 times."
25
+ )
26
+ end
27
+
28
+ it "can't be called when attempt! is called 3 times" do
29
+ subject.foobar
30
+ subject.foobar
31
+ expect {
32
+ subject.foobar
33
+ }.to raise_error(RR::Errors::TimesCalledError, "foobar()\nCalled 3 times.\nExpected 2 times.")
34
+ expect {
35
+ RR.verify
36
+ }.to raise_error(RR::Errors::TimesCalledError, "foobar()\nCalled 3 times.\nExpected 2 times.")
37
+ end
38
+
39
+ it "has a backtrace to where the TimesCalledExpectation was instantiated on failure" do
40
+ error = nil
41
+ begin
42
+ RR.verify
43
+ rescue RR::Errors::TimesCalledError => e
44
+ error = e
45
+ end
46
+ expect(e.backtrace.join("\n")).to include(__FILE__)
47
+ end
48
+
49
+ it "has an error message that includes the number of times called and expected number of times" do
50
+ expect {
51
+ RR.verify
52
+ }.to raise_error(RR::Errors::TimesCalledError, "foobar()\nCalled 0 times.\nExpected 2 times.")
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../../../spec_helper")
2
+
3
+ module RR
4
+ module Expectations
5
+ describe TimesCalledExpectation do
6
+ context "when using a ProcMatcher" do
7
+ include_examples "RR::Expectations::TimesCalledExpectation"
8
+
9
+ before do
10
+ stub(subject).foobar.times(lambda {|value| value == 2})
11
+ end
12
+
13
+ describe "#verify" do
14
+ it "passes after attempt! called 2 times" do
15
+ subject.foobar
16
+ subject.foobar
17
+ RR.verify
18
+ end
19
+
20
+ it "fails after attempt! called 1 time" do
21
+ subject.foobar
22
+ expect { RR.verify }.to raise_error(RR::Errors::TimesCalledError)
23
+ end
24
+
25
+ it "fails after attempt! called 3 times" do
26
+ subject.foobar
27
+ subject.foobar
28
+ subject.foobar
29
+ expect { RR.verify }.to raise_error(RR::Errors::TimesCalledError)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,39 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../../../spec_helper")
2
+
3
+ module RR
4
+ module Expectations
5
+ describe TimesCalledExpectation do
6
+ context "when using a RangeMatcher" do
7
+ include_examples "RR::Expectations::TimesCalledExpectation"
8
+
9
+ before do
10
+ stub(subject).foobar.times(1..2)
11
+ end
12
+
13
+ describe "#verify" do
14
+ it "passes after attempt! called 1 time" do
15
+ subject.foobar
16
+ RR.verify
17
+ end
18
+
19
+ it "passes after attempt! called 2 times" do
20
+ subject.foobar
21
+ subject.foobar
22
+ RR.verify
23
+ end
24
+
25
+ it "can't be called when attempt! is called 3 times" do
26
+ subject.foobar
27
+ subject.foobar
28
+ expect {
29
+ subject.foobar
30
+ }.to raise_error(RR::Errors::TimesCalledError, "foobar()\nCalled 3 times.\nExpected 1..2 times.")
31
+ expect {
32
+ RR.verify
33
+ }.to raise_error(RR::Errors::TimesCalledError, "foobar()\nCalled 3 times.\nExpected 1..2 times.")
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,88 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
2
+
3
+ module RR
4
+ describe HashWithObjectIdKey do
5
+ describe "#[] and #[]=" do
6
+ it "stores object via object id" do
7
+ hash = HashWithObjectIdKey.new
8
+ array_1 = []
9
+ hash[array_1] = 1
10
+ array_2 = []
11
+ hash[array_2] = 2
12
+
13
+ expect(hash[array_1]).to_not eq hash[array_2]
14
+ end
15
+
16
+ it "stores the passed in object" do
17
+ hash = HashWithObjectIdKey.new
18
+ obj = Object.new
19
+ hash[obj] = 1
20
+ expect(hash.instance_eval {@keys}).to eq({obj.__id__ => obj})
21
+ end
22
+ end
23
+
24
+ describe "#each" do
25
+ it "iterates through the items in the hash" do
26
+ hash = HashWithObjectIdKey.new
27
+ hash['one'] = 1
28
+ hash['two'] = 2
29
+
30
+ keys = []
31
+ values = []
32
+ hash.each do |key, value|
33
+ keys << key
34
+ values << value
35
+ end
36
+
37
+ expect(keys.sort).to eq ['one', 'two']
38
+ expect(values.sort).to eq [1, 2]
39
+ end
40
+ end
41
+
42
+ describe "#delete" do
43
+ before do
44
+ @hash = HashWithObjectIdKey.new
45
+ @key = Object.new
46
+ @hash[@key] = 1
47
+ end
48
+
49
+ it "removes the object from the hash" do
50
+ @hash.delete(@key)
51
+ expect(@hash[@key]).to be_nil
52
+ end
53
+
54
+ it "removes the object from the keys hash" do
55
+ @hash.delete(@key)
56
+ expect(@hash.instance_eval { @keys }).to eq({})
57
+ end
58
+ end
59
+
60
+ describe "#keys" do
61
+ before do
62
+ @hash = HashWithObjectIdKey.new
63
+ @key = Object.new
64
+ @hash[@key] = 1
65
+ end
66
+
67
+ it "returns an array of the keys" do
68
+ expect(@hash.keys).to eq [@key]
69
+ end
70
+ end
71
+
72
+ describe "#include?" do
73
+ before do
74
+ @hash = HashWithObjectIdKey.new
75
+ @key = Object.new
76
+ @hash[@key] = 1
77
+ end
78
+
79
+ it "returns true when the key is in the Hash" do
80
+ expect(@hash.include?(@key)).to be_true
81
+ end
82
+
83
+ it "returns false when the key is not in the Hash" do
84
+ expect(@hash.include?(Object.new)).to be_false
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,545 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../../../spec_helper")
2
+
3
+ module RR
4
+ module Injections
5
+ describe DoubleInjection do
6
+ attr_reader :method_name, :double_injection
7
+
8
+ macro("sets up subject and method_name") do
9
+ it "sets up subject and method_name" do
10
+ expect(double_injection.subject).to equal subject
11
+ expect(double_injection.method_name).to eq method_name.to_sym
12
+ end
13
+ end
14
+
15
+ subject { Object.new }
16
+
17
+ describe "mock/stub" do
18
+ context "when the subject responds to the injected method" do
19
+ before do
20
+ class << subject
21
+ attr_reader :original_foobar_called
22
+
23
+ def foobar
24
+ @original_foobar_called = true
25
+ :original_foobar
26
+ end
27
+ end
28
+
29
+ expect(subject).to respond_to(:foobar)
30
+ expect(!!subject.methods.detect {|method| method.to_sym == :foobar}).to be_true
31
+ stub(subject).foobar {:new_foobar}
32
+ end
33
+
34
+ describe "being bound" do
35
+ it "sets __rr__original_{method_name} to the original method" do
36
+ expect(subject.__rr__original_foobar).to eq :original_foobar
37
+ end
38
+
39
+ describe "being called" do
40
+ it "returns the return value of the block" do
41
+ expect(subject.foobar).to eq :new_foobar
42
+ end
43
+
44
+ it "does not call the original method" do
45
+ subject.foobar
46
+ expect(subject.original_foobar_called).to be_nil
47
+ end
48
+ end
49
+
50
+ describe "being reset" do
51
+ before do
52
+ RR::Space.reset_double(subject, :foobar)
53
+ end
54
+
55
+ it "rebinds the original method" do
56
+ expect(subject.foobar).to eq :original_foobar
57
+ end
58
+
59
+ it "removes __rr__original_{method_name}" do
60
+ subject.should_not respond_to(:__rr__original_foobar)
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ context "when the subject does not respond to the injected method" do
67
+ before do
68
+ subject.should_not respond_to(:foobar)
69
+ subject.methods.should_not include('foobar')
70
+ stub(subject).foobar {:new_foobar}
71
+ end
72
+
73
+ it "does not set __rr__original_{method_name} to the original method" do
74
+ subject.should_not respond_to(:__rr__original_foobar)
75
+ end
76
+
77
+ describe "being called" do
78
+ it "calls the newly defined method" do
79
+ expect(subject.foobar).to eq :new_foobar
80
+ end
81
+ end
82
+
83
+ describe "being reset" do
84
+ before do
85
+ RR::Space.reset_double(subject, :foobar)
86
+ end
87
+
88
+ it "unsets the foobar method" do
89
+ subject.should_not respond_to(:foobar)
90
+ subject.methods.should_not include('foobar')
91
+ end
92
+ end
93
+ end
94
+
95
+ context "when the subject redefines respond_to?" do
96
+ it "does not try to call the implementation" do
97
+ class << subject
98
+ def respond_to?(method_symbol, include_private = false)
99
+ method_symbol == :foobar
100
+ end
101
+ end
102
+ mock(subject).foobar
103
+ expect(subject.foobar).to eq nil
104
+ end
105
+ end
106
+ end
107
+
108
+ describe "mock/stub + proxy" do
109
+ context "when the subject responds to the injected method" do
110
+ context "when the subject has the method defined" do
111
+ describe "being bound" do
112
+ before do
113
+ def subject.foobar
114
+ :original_foobar
115
+ end
116
+
117
+ expect(subject).to respond_to(:foobar)
118
+ expect(!!subject.methods.detect {|method| method.to_sym == :foobar}).to be_true
119
+ stub.proxy(subject).foobar {:new_foobar}
120
+ end
121
+
122
+ it "aliases the original method to __rr__original_{method_name}" do
123
+ expect(subject.__rr__original_foobar).to eq :original_foobar
124
+ end
125
+
126
+ it "replaces the original method with the new method" do
127
+ expect(subject.foobar).to eq :new_foobar
128
+ end
129
+
130
+ describe "being called" do
131
+ it "calls the original method first and sends it into the block" do
132
+ original_return_value = nil
133
+ stub.proxy(subject).foobar {|arg| original_return_value = arg; :new_foobar}
134
+ expect(subject.foobar).to eq :new_foobar
135
+ expect(original_return_value).to eq :original_foobar
136
+ end
137
+ end
138
+
139
+ describe "being reset" do
140
+ before do
141
+ RR::Space.reset_double(subject, :foobar)
142
+ end
143
+
144
+ it "rebinds the original method" do
145
+ expect(subject.foobar).to eq :original_foobar
146
+ end
147
+
148
+ it "removes __rr__original_{method_name}" do
149
+ subject.should_not respond_to(:__rr__original_foobar)
150
+ end
151
+ end
152
+ end
153
+ end
154
+
155
+ context "when the subject does not have the method defined" do
156
+ describe "being bound" do
157
+ context "when the subject has not been previously bound to" do
158
+ before do
159
+ setup_subject
160
+
161
+ expect(subject).to respond_to(:foobar)
162
+ stub.proxy(subject).foobar {:new_foobar}
163
+ end
164
+
165
+ def setup_subject
166
+ def subject.respond_to?(method_name)
167
+ if method_name.to_sym == :foobar
168
+ true
169
+ else
170
+ super
171
+ end
172
+ end
173
+ end
174
+
175
+ it "does not define __rr__original_{method_name}" do
176
+ subject.methods.should_not include("__rr__original_foobar")
177
+ end
178
+
179
+ context "when method is defined after being bound and before being called" do
180
+ def setup_subject
181
+ super
182
+ def subject.foobar
183
+ :original_foobar
184
+ end
185
+ end
186
+
187
+ describe "being called" do
188
+ it "defines __rr__original_{method_name} to be the lazily created method" do
189
+ expect((!!subject.methods.detect {|method| method.to_sym == :__rr__original_foobar})).to be_true
190
+ expect(subject.__rr__original_foobar).to eq :original_foobar
191
+ end
192
+
193
+ it "calls the original method first and sends it into the block" do
194
+ original_return_value = nil
195
+ stub.proxy(subject).foobar {|arg| original_return_value = arg; :new_foobar}
196
+ expect(subject.foobar).to eq :new_foobar
197
+ expect(original_return_value).to eq :original_foobar
198
+ end
199
+ end
200
+
201
+ describe "being reset" do
202
+ before do
203
+ RR::Space.reset_double(subject, :foobar)
204
+ end
205
+
206
+ it "rebinds the original method" do
207
+ expect(subject.foobar).to eq :original_foobar
208
+ end
209
+
210
+ it "removes __rr__original_{method_name}" do
211
+ subject.should_not respond_to(:__rr__original_foobar)
212
+ end
213
+ end
214
+ end
215
+
216
+ context "when method is still not defined" do
217
+ context "when the method is lazily created" do
218
+ def setup_subject
219
+ super
220
+ def subject.method_missing(method_name, *args, &block)
221
+ if method_name.to_sym == :foobar
222
+ def self.foobar
223
+ :original_foobar
224
+ end
225
+
226
+ foobar
227
+ else
228
+ super
229
+ end
230
+ end
231
+ end
232
+
233
+ describe "being called" do
234
+ it "defines __rr__original_{method_name} to be the lazily created method" do
235
+ subject.foobar
236
+ expect((!!subject.methods.detect {|method| method.to_sym == :__rr__original_foobar})).to be_true
237
+ expect(subject.__rr__original_foobar).to eq :original_foobar
238
+ end
239
+
240
+ it "calls the lazily created method and returns the injected method return value" do
241
+ original_return_value = nil
242
+ stub.proxy(subject).foobar {|arg| original_return_value = arg; :new_foobar}
243
+ expect(subject.foobar).to eq :new_foobar
244
+ expect(original_return_value).to eq :original_foobar
245
+ end
246
+ end
247
+
248
+ describe "being reset" do
249
+ context "when reset before being called" do
250
+ before do
251
+ RR::Space.reset_double(subject, :foobar)
252
+ end
253
+
254
+ it "rebinds the original method" do
255
+ expect(subject.foobar).to eq :original_foobar
256
+ end
257
+
258
+ it "removes __rr__original_{method_name}" do
259
+ subject.should_not respond_to(:__rr__original_foobar)
260
+ end
261
+ end
262
+ end
263
+ end
264
+
265
+ context "when the method is not lazily created (handled in method_missing)" do
266
+ def setup_subject
267
+ super
268
+ def subject.method_missing(method_name, *args, &block)
269
+ if method_name.to_sym == :foobar
270
+ :original_foobar
271
+ else
272
+ super
273
+ end
274
+ end
275
+ end
276
+
277
+ describe "being called" do
278
+ it "does not define the __rr__original_{method_name}" do
279
+ subject.foobar
280
+ subject.methods.should_not include("__rr__original_foobar")
281
+ end
282
+
283
+ it "calls the lazily created method and returns the injected method return value" do
284
+ original_return_value = nil
285
+ stub.proxy(subject).foobar {|arg| original_return_value = arg; :new_foobar}
286
+ expect(subject.foobar).to eq :new_foobar
287
+ expect(original_return_value).to eq :original_foobar
288
+ end
289
+ end
290
+
291
+ describe "being reset" do
292
+ before do
293
+ RR::Space.reset_double(subject, :foobar)
294
+ end
295
+
296
+ it "rebinds the original method" do
297
+ expect(subject.foobar).to eq :original_foobar
298
+ end
299
+
300
+ it "removes __rr__original_{method_name}" do
301
+ subject.should_not respond_to(:__rr__original_foobar)
302
+ end
303
+ end
304
+ end
305
+ end
306
+ end
307
+
308
+ context "when the subject has been previously bound to" do
309
+ before do
310
+ setup_subject
311
+
312
+ expect(subject).to respond_to(:foobar)
313
+ stub.proxy(subject).baz {:new_baz}
314
+ stub.proxy(subject).foobar {:new_foobar}
315
+ end
316
+
317
+ def setup_subject
318
+ def subject.respond_to?(method_name)
319
+ if method_name.to_sym == :foobar || method_name.to_sym == :baz
320
+ true
321
+ else
322
+ super
323
+ end
324
+ end
325
+ end
326
+
327
+ it "does not define __rr__original_{method_name}" do
328
+ subject.methods.should_not include("__rr__original_foobar")
329
+ end
330
+
331
+ context "when method is defined after being bound and before being called" do
332
+ def setup_subject
333
+ super
334
+ def subject.foobar
335
+ :original_foobar
336
+ end
337
+ end
338
+
339
+ describe "being called" do
340
+ it "defines __rr__original_{method_name} to be the lazily created method" do
341
+ expect((!!subject.methods.detect {|method| method.to_sym == :__rr__original_foobar})).to be_true
342
+ expect(subject.__rr__original_foobar).to eq :original_foobar
343
+ end
344
+
345
+ it "calls the original method first and sends it into the block" do
346
+ original_return_value = nil
347
+ stub.proxy(subject).foobar {|arg| original_return_value = arg; :new_foobar}
348
+ expect(subject.foobar).to eq :new_foobar
349
+ expect(original_return_value).to eq :original_foobar
350
+ end
351
+ end
352
+
353
+ describe "being reset" do
354
+ before do
355
+ RR::Space.reset_double(subject, :foobar)
356
+ end
357
+
358
+ it "rebinds the original method" do
359
+ expect(subject.foobar).to eq :original_foobar
360
+ end
361
+
362
+ it "removes __rr__original_{method_name}" do
363
+ subject.should_not respond_to(:__rr__original_foobar)
364
+ end
365
+ end
366
+ end
367
+
368
+ context "when method is still not defined" do
369
+ context "when the method is lazily created" do
370
+ def setup_subject
371
+ super
372
+ def subject.method_missing(method_name, *args, &block)
373
+ if method_name.to_sym == :foobar
374
+ def self.foobar
375
+ :original_foobar
376
+ end
377
+
378
+ foobar
379
+ else
380
+ super
381
+ end
382
+ end
383
+ end
384
+
385
+ describe "being called" do
386
+ it "defines __rr__original_{method_name} to be the lazily created method" do
387
+ subject.foobar
388
+ expect((!!subject.methods.detect {|method| method.to_sym == :__rr__original_foobar})).to be_true
389
+ expect(subject.__rr__original_foobar).to eq :original_foobar
390
+ end
391
+
392
+ it "calls the lazily created method and returns the injected method return value" do
393
+ original_return_value = nil
394
+ stub.proxy(subject).foobar {|arg| original_return_value = arg; :new_foobar}
395
+ expect(subject.foobar).to eq :new_foobar
396
+ expect(original_return_value).to eq :original_foobar
397
+ end
398
+ end
399
+
400
+ describe "being reset" do
401
+ context "when reset before being called" do
402
+ before do
403
+ RR::Space.reset_double(subject, :foobar)
404
+ end
405
+
406
+ it "rebinds the original method" do
407
+ expect(subject.foobar).to eq :original_foobar
408
+ end
409
+
410
+ it "removes __rr__original_{method_name}" do
411
+ subject.should_not respond_to(:__rr__original_foobar)
412
+ end
413
+ end
414
+ end
415
+ end
416
+
417
+ context "when the method is not lazily created (handled in method_missing)" do
418
+ def setup_subject
419
+ super
420
+ def subject.method_missing(method_name, *args, &block)
421
+ if method_name.to_sym == :foobar
422
+ :original_foobar
423
+ else
424
+ super
425
+ end
426
+ end
427
+ end
428
+
429
+ describe "being called" do
430
+ it "does not define the __rr__original_{method_name}" do
431
+ subject.foobar
432
+ subject.methods.should_not include("__rr__original_foobar")
433
+ end
434
+
435
+ it "calls the lazily created method and returns the injected method return value" do
436
+ original_return_value = nil
437
+ stub.proxy(subject).foobar {|arg| original_return_value = arg; :new_foobar}
438
+ expect(subject.foobar).to eq :new_foobar
439
+ expect(original_return_value).to eq :original_foobar
440
+ end
441
+ end
442
+
443
+ describe "being reset" do
444
+ before do
445
+ RR::Space.reset_double(subject, :foobar)
446
+ end
447
+
448
+ it "rebinds the original method" do
449
+ expect(subject.foobar).to eq :original_foobar
450
+ end
451
+
452
+ it "removes __rr__original_{method_name}" do
453
+ subject.should_not respond_to(:__rr__original_foobar)
454
+ end
455
+ end
456
+ end
457
+ end
458
+ end
459
+ end
460
+ end
461
+ end
462
+
463
+ context "when the subject does not respond to the injected method" do
464
+ context "when the subject responds to the method via method_missing" do
465
+ describe "being bound" do
466
+ before do
467
+ subject.should_not respond_to(:foobar)
468
+ subject.methods.should_not include('foobar')
469
+ class << subject
470
+ def method_missing(method_name, *args, &block)
471
+ if method_name == :foobar
472
+ :original_foobar
473
+ else
474
+ super
475
+ end
476
+ end
477
+ end
478
+ stub.proxy(subject).foobar {:new_foobar}
479
+ end
480
+
481
+ it "adds the method to the subject" do
482
+ expect(subject).to respond_to(:foobar)
483
+ expect((!!subject.methods.detect {|method| method.to_sym == :foobar})).to be_true
484
+ end
485
+
486
+ describe "being called" do
487
+ it "calls the original method first and sends it into the block" do
488
+ original_return_value = nil
489
+ stub.proxy(subject).foobar {|arg| original_return_value = arg; :new_foobar}
490
+ expect(subject.foobar).to eq :new_foobar
491
+ expect(original_return_value).to eq :original_foobar
492
+ end
493
+ end
494
+
495
+ describe "being reset" do
496
+ before do
497
+ RR::Space.reset_double(subject, :foobar)
498
+ end
499
+
500
+ it "unsets the foobar method" do
501
+ subject.should_not respond_to(:foobar)
502
+ subject.methods.should_not include('foobar')
503
+ end
504
+ end
505
+ end
506
+ end
507
+
508
+ context "when the subject would raise a NoMethodError when the method is called" do
509
+ describe "being bound" do
510
+ before do
511
+ subject.should_not respond_to(:foobar)
512
+ subject.methods.should_not include('foobar')
513
+ stub.proxy(subject).foobar {:new_foobar}
514
+ end
515
+
516
+ it "adds the method to the subject" do
517
+ expect(subject).to respond_to(:foobar)
518
+ expect((!!subject.methods.detect {|method| method.to_sym == :foobar})).to be_true
519
+ end
520
+
521
+ describe "being called" do
522
+ it "raises a NoMethodError" do
523
+ expect {
524
+ subject.foobar
525
+ }.to raise_error(NoMethodError)
526
+ end
527
+ end
528
+
529
+ describe "being reset" do
530
+ before do
531
+ RR::Space.reset_double(subject, :foobar)
532
+ end
533
+
534
+ it "unsets the foobar method" do
535
+ subject.should_not respond_to(:foobar)
536
+ subject.methods.should_not include('foobar')
537
+ end
538
+ end
539
+ end
540
+ end
541
+ end
542
+ end
543
+ end
544
+ end
545
+ end