rr 1.0.5 → 1.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +6 -14
  2. data/CHANGES.md +24 -0
  3. data/LICENSE +2 -2
  4. data/README.md +124 -741
  5. data/VERSION +1 -1
  6. data/lib/rr.rb +2 -103
  7. data/lib/rr/adapters/minitest.rb +21 -13
  8. data/lib/rr/adapters/minitest_active_support.rb +34 -0
  9. data/lib/rr/adapters/none.rb +17 -0
  10. data/lib/rr/adapters/{rspec.rb → rspec/invocation_matcher.rb} +2 -27
  11. data/lib/rr/adapters/rspec_1.rb +42 -0
  12. data/lib/rr/adapters/rspec_2.rb +24 -0
  13. data/lib/rr/adapters/test_unit_1.rb +54 -0
  14. data/lib/rr/adapters/test_unit_2.rb +13 -0
  15. data/lib/rr/adapters/test_unit_2_active_support.rb +35 -0
  16. data/lib/rr/autohook.rb +43 -0
  17. data/lib/rr/core_ext/array.rb +12 -0
  18. data/lib/rr/core_ext/enumerable.rb +16 -0
  19. data/lib/rr/core_ext/hash.rb +20 -0
  20. data/lib/rr/core_ext/range.rb +8 -0
  21. data/lib/rr/core_ext/regexp.rb +8 -0
  22. data/lib/rr/double.rb +4 -4
  23. data/lib/rr/double_definitions/double_definition.rb +9 -3
  24. data/lib/rr/errors.rb +21 -0
  25. data/lib/rr/expectations/argument_equality_expectation.rb +10 -7
  26. data/lib/rr/expectations/times_called_expectation.rb +2 -8
  27. data/lib/rr/injections/double_injection.rb +1 -1
  28. data/lib/rr/method_dispatches/base_method_dispatch.rb +1 -1
  29. data/lib/rr/recorded_calls.rb +12 -12
  30. data/lib/rr/space.rb +5 -3
  31. data/lib/rr/times_called_matchers/never_matcher.rb +2 -2
  32. data/lib/rr/wildcard_matchers/anything.rb +2 -2
  33. data/lib/rr/wildcard_matchers/boolean.rb +3 -7
  34. data/lib/rr/wildcard_matchers/duck_type.rb +11 -15
  35. data/lib/rr/wildcard_matchers/hash_including.rb +14 -13
  36. data/lib/rr/wildcard_matchers/is_a.rb +6 -7
  37. data/lib/rr/wildcard_matchers/satisfy.rb +8 -8
  38. data/lib/rr/without_autohook.rb +112 -0
  39. data/rr.gemspec +28 -0
  40. data/spec/global_helper.rb +12 -0
  41. data/spec/suite.rb +93 -0
  42. data/spec/suites/common/adapter_tests.rb +37 -0
  43. data/spec/suites/common/rails_integration_test.rb +175 -0
  44. data/spec/suites/common/test_unit_tests.rb +25 -0
  45. data/spec/suites/minitest/integration/minitest_test.rb +13 -0
  46. data/spec/suites/minitest/test_helper.rb +3 -0
  47. data/spec/suites/rspec_1/integration/rspec_1_spec.rb +20 -0
  48. data/spec/suites/rspec_1/integration/test_unit_1_rails_spec.rb +19 -0
  49. data/spec/suites/rspec_1/integration/test_unit_2_rails_spec.rb +18 -0
  50. data/spec/suites/rspec_1/spec_helper.rb +24 -0
  51. data/spec/suites/rspec_2/functional/any_instance_of_spec.rb +47 -0
  52. data/spec/suites/rspec_2/functional/dont_allow_spec.rb +12 -0
  53. data/spec/suites/rspec_2/functional/dsl_spec.rb +13 -0
  54. data/spec/suites/rspec_2/functional/instance_of_spec.rb +14 -0
  55. data/spec/suites/rspec_2/functional/mock_spec.rb +241 -0
  56. data/spec/suites/rspec_2/functional/proxy_spec.rb +136 -0
  57. data/spec/suites/rspec_2/functional/spy_spec.rb +41 -0
  58. data/spec/suites/rspec_2/functional/strong_spec.rb +79 -0
  59. data/spec/suites/rspec_2/functional/stub_spec.rb +190 -0
  60. data/spec/suites/rspec_2/functional/wildcard_matchers_spec.rb +128 -0
  61. data/spec/suites/rspec_2/integration/minitest_rails_spec.rb +15 -0
  62. data/spec/suites/rspec_2/integration/rspec_2_spec.rb +20 -0
  63. data/spec/suites/rspec_2/integration/test_unit_rails_spec.rb +14 -0
  64. data/spec/suites/rspec_2/spec_helper.rb +27 -0
  65. data/spec/suites/rspec_2/support/matchers/wildcard_matcher_matchers.rb +32 -0
  66. data/spec/suites/rspec_2/support/shared_examples/space.rb +13 -0
  67. data/spec/suites/rspec_2/support/shared_examples/times_called_expectation.rb +9 -0
  68. data/spec/suites/rspec_2/unit/adapters/rr_methods/double_creators_spec.rb +135 -0
  69. data/spec/suites/rspec_2/unit/adapters/rr_methods/space_spec.rb +101 -0
  70. data/spec/suites/rspec_2/unit/adapters/rr_methods/wildcard_matchers_spec.rb +69 -0
  71. data/spec/suites/rspec_2/unit/adapters/rspec/invocation_matcher_spec.rb +297 -0
  72. data/spec/suites/rspec_2/unit/adapters/rspec_spec.rb +85 -0
  73. data/spec/suites/rspec_2/unit/core_ext/array_spec.rb +39 -0
  74. data/spec/suites/rspec_2/unit/core_ext/enumerable_spec.rb +81 -0
  75. data/spec/suites/rspec_2/unit/core_ext/hash_spec.rb +55 -0
  76. data/spec/suites/rspec_2/unit/core_ext/range_spec.rb +41 -0
  77. data/spec/suites/rspec_2/unit/core_ext/regexp_spec.rb +41 -0
  78. data/spec/suites/rspec_2/unit/double_definitions/child_double_definition_create_spec.rb +114 -0
  79. data/spec/suites/rspec_2/unit/double_definitions/double_definition_create_blank_slate_spec.rb +93 -0
  80. data/spec/suites/rspec_2/unit/double_definitions/double_definition_create_spec.rb +446 -0
  81. data/spec/suites/rspec_2/unit/errors/rr_error_spec.rb +67 -0
  82. data/spec/suites/rspec_2/unit/expectations/any_argument_expectation_spec.rb +48 -0
  83. data/spec/suites/rspec_2/unit/expectations/anything_argument_equality_expectation_spec.rb +14 -0
  84. data/spec/suites/rspec_2/unit/expectations/argument_equality_expectation_spec.rb +135 -0
  85. data/spec/suites/rspec_2/unit/expectations/boolean_argument_equality_expectation_spec.rb +30 -0
  86. data/spec/suites/rspec_2/unit/expectations/hash_including_argument_equality_expectation_spec.rb +82 -0
  87. data/spec/suites/rspec_2/unit/expectations/satisfy_argument_equality_expectation_spec.rb +61 -0
  88. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/any_times_matcher_spec.rb +22 -0
  89. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/at_least_matcher_spec.rb +37 -0
  90. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/at_most_matcher_spec.rb +43 -0
  91. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/integer_matcher_spec.rb +58 -0
  92. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/proc_matcher_spec.rb +35 -0
  93. data/spec/suites/rspec_2/unit/expectations/times_called_expectation/range_matcher_spec.rb +39 -0
  94. data/spec/suites/rspec_2/unit/hash_with_object_id_key_spec.rb +88 -0
  95. data/spec/suites/rspec_2/unit/injections/double_injection/double_injection_spec.rb +545 -0
  96. data/spec/suites/rspec_2/unit/injections/double_injection/double_injection_verify_spec.rb +32 -0
  97. data/spec/suites/rspec_2/unit/proc_from_block_spec.rb +14 -0
  98. data/spec/suites/rspec_2/unit/rr_spec.rb +28 -0
  99. data/spec/suites/rspec_2/unit/space_spec.rb +595 -0
  100. data/spec/suites/rspec_2/unit/spy_verification_spec.rb +133 -0
  101. data/spec/suites/rspec_2/unit/times_called_matchers/any_times_matcher_spec.rb +46 -0
  102. data/spec/suites/rspec_2/unit/times_called_matchers/at_least_matcher_spec.rb +54 -0
  103. data/spec/suites/rspec_2/unit/times_called_matchers/at_most_matcher_spec.rb +69 -0
  104. data/spec/suites/rspec_2/unit/times_called_matchers/integer_matcher_spec.rb +69 -0
  105. data/spec/suites/rspec_2/unit/times_called_matchers/proc_matcher_spec.rb +54 -0
  106. data/spec/suites/rspec_2/unit/times_called_matchers/range_matcher_spec.rb +75 -0
  107. data/spec/suites/rspec_2/unit/times_called_matchers/times_called_matcher_spec.rb +117 -0
  108. data/spec/suites/rspec_2/unit/wildcard_matchers/anything_spec.rb +33 -0
  109. data/spec/suites/rspec_2/unit/wildcard_matchers/boolean_spec.rb +45 -0
  110. data/spec/suites/rspec_2/unit/wildcard_matchers/duck_type_spec.rb +64 -0
  111. data/spec/suites/rspec_2/unit/wildcard_matchers/hash_including_spec.rb +64 -0
  112. data/spec/suites/rspec_2/unit/wildcard_matchers/is_a_spec.rb +55 -0
  113. data/spec/suites/rspec_2/unit/wildcard_matchers/numeric_spec.rb +46 -0
  114. data/spec/suites/rspec_2/unit/wildcard_matchers/satisfy_spec.rb +57 -0
  115. data/spec/suites/test_unit_1/integration/test_unit_1_test.rb +6 -0
  116. data/spec/suites/test_unit_1/test_helper.rb +7 -0
  117. data/spec/suites/test_unit_2/integration/test_unit_2_test.rb +6 -0
  118. data/spec/suites/test_unit_2/test_helper.rb +3 -0
  119. metadata +183 -19
  120. data/Gemfile +0 -9
  121. data/Rakefile +0 -34
  122. data/lib/rr/adapters/rspec2.rb +0 -30
  123. data/lib/rr/adapters/test_unit.rb +0 -33
  124. data/lib/rr/errors/argument_equality_error.rb +0 -6
  125. data/lib/rr/wildcard_matchers/range.rb +0 -7
  126. data/lib/rr/wildcard_matchers/regexp.rb +0 -7
  127. data/spec/runner.rb +0 -41
@@ -0,0 +1,55 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe Hash do
4
+ include WildcardMatcherMatchers
5
+
6
+ describe '#wildcard_match?' do
7
+ context 'when this Hash has keys that respond to #wildcard_match?' do
8
+ subject { {is_a(Symbol) => 'x'} }
9
+
10
+ it "returns true if all keys in the given Hash wildcard-match the corresponding keys in this Hash" do
11
+ should wildcard_match({:foo => 'x'})
12
+ end
13
+
14
+ it "returns true if any keys in the given Hash do not wildcard-match the corresponding keys in this Hash" do
15
+ should_not wildcard_match({'foo' => 'x'})
16
+ end
17
+ end
18
+
19
+ context 'when this Hash has values that respond to #wildcard_match?' do
20
+ subject { {'x' => is_a(Symbol)} }
21
+
22
+ it "returns true if all values in the given Hash wildcard-match the corresponding values in this Hash" do
23
+ should wildcard_match({'x' => :foo})
24
+ end
25
+
26
+ it "returns false if any values in the given Hash do not wildcard-match the corresponding values in this Hash" do
27
+ should_not wildcard_match({'x' => 'foo'})
28
+ end
29
+ end
30
+
31
+ context 'when this Hash does not have keys or values that respond to #wildcard_match?' do
32
+ subject { {:x => :y} }
33
+
34
+ it "returns true if all pairs in the given Hash wildcard-match the corresponding values in this Hash" do
35
+ should wildcard_match({:x => :y})
36
+ end
37
+
38
+ it "returns false if any keys do not equal-match corresponding items in the subject" do
39
+ should_not wildcard_match({:z => :y})
40
+ end
41
+
42
+ it "returns false if any values do not equal-match corresponding items in the subject" do
43
+ should_not wildcard_match({:x => :z})
44
+ end
45
+ end
46
+
47
+ context 'when not given a Hash' do
48
+ subject { {:foo => 'bar'} }
49
+
50
+ it "returns false" do
51
+ should_not wildcard_match(:something_else)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
2
+
3
+ describe Range do
4
+ include WildcardMatcherMatchers
5
+
6
+ describe '#wildcard_match?' do
7
+ subject { 1..5 }
8
+
9
+ it "returns true when given Range is exactly equal to this Range" do
10
+ should wildcard_match(1..5)
11
+ end
12
+
13
+ it "returns true when given number falls within the range" do
14
+ should wildcard_match(3)
15
+ end
16
+
17
+ it "returns false when given number falls outside the range" do
18
+ should_not wildcard_match(8)
19
+ end
20
+ end
21
+
22
+ describe '#==' do
23
+ subject { 1..5 }
24
+
25
+ it "returns true when given Range is exactly equal to this Range" do
26
+ should equal_match(1..5)
27
+ end
28
+
29
+ it "returns false when given Range is not exactly equal to this Range" do
30
+ should_not equal_match(3..5)
31
+ end
32
+
33
+ it "returns false even when given an object that wildcard matches this Range" do
34
+ should_not equal_match(3)
35
+ end
36
+
37
+ it "returns false when given object isn't even a Range" do
38
+ should_not equal_match(:something_else)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe Regexp do
4
+ include WildcardMatcherMatchers
5
+
6
+ describe '#wildcard_match?' do
7
+ subject { /foo/ }
8
+
9
+ it "returns true when given Regexp is exactly equal to this Regexp" do
10
+ should wildcard_match(/foo/)
11
+ end
12
+
13
+ it "returns true if given string matches the regexp" do
14
+ should wildcard_match('foobarbaz')
15
+ end
16
+
17
+ it "returns false if given string does not match the regexp" do
18
+ should_not wildcard_match('aslkj')
19
+ end
20
+ end
21
+
22
+ describe '#==' do
23
+ subject { /foo/ }
24
+
25
+ it "returns true when given Regexp is exactly equal to this Regexp" do
26
+ should equal_match(/foo/)
27
+ end
28
+
29
+ it "returns false when given Regexp is not exactly equal to this Regexp" do
30
+ should_not equal_match(/alkj/)
31
+ end
32
+
33
+ it "returns false even when given an object that wildcard matches this Regexp" do
34
+ should_not equal_match('foobarbaz')
35
+ end
36
+
37
+ it "returns false when not even given a Regexp" do
38
+ should_not equal_match(:something_else)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,114 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
2
+
3
+ module RR
4
+ module DoubleDefinitions
5
+ describe ChildDoubleDefinitionCreate do
6
+ attr_reader :parent_subject, :parent_double_definition_create, :parent_double_definition, :child_double_definition_create
7
+
8
+ include_examples "Swapped Space"
9
+
10
+ before(:each) do
11
+ @parent_subject = Object.new
12
+ @parent_double_definition_create = DoubleDefinitionCreate.new
13
+ @parent_double_definition = DoubleDefinition.new(parent_double_definition_create)
14
+ @child_double_definition_create = ChildDoubleDefinitionCreate.new(parent_double_definition)
15
+ end
16
+
17
+ describe "#root_subject" do
18
+ it "returns the #parent_double_definition.root_subject" do
19
+ child_subject = Object.new
20
+ parent_double_definition_create.stub(parent_subject)
21
+ child_double_definition_create.stub(child_subject)
22
+ expect(child_double_definition_create.root_subject).to eq parent_subject
23
+ end
24
+ end
25
+
26
+ describe "Strategies::Verification definitions" do
27
+ describe "methods without !" do
28
+ attr_reader :child_subject
29
+ before do
30
+ @child_subject = Object.new
31
+ end
32
+
33
+ describe "#mock" do
34
+ context "when passed a subject" do
35
+ it "sets #parent_double_definition.implementation to a Proc returning the passed-in subject" do
36
+ expect(parent_double_definition.implementation).to be_nil
37
+ child_double_definition_create.mock(child_subject)
38
+ expect(parent_double_definition.implementation.call).to eq child_subject
39
+ end
40
+ end
41
+ end
42
+
43
+ describe "#stub" do
44
+ context "when passed a subject" do
45
+ it "sets #parent_double_definition.implementation to a Proc returning the passed-in subject" do
46
+ expect(parent_double_definition.implementation).to be_nil
47
+ child_double_definition_create.stub(child_subject)
48
+ expect(parent_double_definition.implementation.call).to eq child_subject
49
+ end
50
+ end
51
+ end
52
+
53
+ describe "#dont_allow" do
54
+ context "when passed a subject" do
55
+ it "sets #parent_double_definition.implementation to a Proc returning the passed-in subject" do
56
+ expect(parent_double_definition.implementation).to be_nil
57
+ child_double_definition_create.dont_allow(child_subject)
58
+ expect(parent_double_definition.implementation.call).to eq child_subject
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "methods with !" do
65
+ describe "#mock!" do
66
+ it "sets #parent_double_definition.implementation to a Proc returning the #subject" do
67
+ expect(parent_double_definition.implementation).to be_nil
68
+ child_subject = child_double_definition_create.mock!.__double_definition_create__.subject
69
+ expect(parent_double_definition.implementation.call).to eq child_subject
70
+ end
71
+ end
72
+
73
+ describe "#stub!" do
74
+ it "sets #parent_double_definition.implementation to a Proc returning the #subject" do
75
+ expect(parent_double_definition.implementation).to be_nil
76
+ child_subject = child_double_definition_create.stub!.__double_definition_create__.subject
77
+ expect(parent_double_definition.implementation.call).to eq child_subject
78
+ end
79
+ end
80
+
81
+ describe "#dont_allow!" do
82
+ it "sets #parent_double_definition.implementation to a Proc returning the #subject" do
83
+ expect(parent_double_definition.implementation).to be_nil
84
+ child_subject = child_double_definition_create.dont_allow!.__double_definition_create__.subject
85
+ expect(parent_double_definition.implementation.call).to eq child_subject
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ describe "Strategies::DoubleInjection definitions" do
92
+ describe "methods without !" do
93
+ describe "#instance_of" do
94
+ it "raises a NoMethodError" do
95
+ expect {
96
+ child_double_definition_create.instance_of
97
+ }.to raise_error(NoMethodError)
98
+ end
99
+ end
100
+ end
101
+
102
+ describe "methods with !" do
103
+ describe "#instance_of!" do
104
+ it "raises a NoMethodError" do
105
+ expect {
106
+ child_double_definition_create.instance_of!
107
+ }.to raise_error(NoMethodError)
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,93 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
2
+
3
+ module RR
4
+ module DoubleDefinitions
5
+ describe DoubleDefinitionCreateBlankSlate do
6
+ attr_reader :double_definition_create, :blank_slate
7
+
8
+ include_examples "Swapped Space"
9
+
10
+ subject { Object.new }
11
+
12
+ before(:each) do
13
+ @double_definition_create = DoubleDefinitionCreate.new
14
+ double_definition_create.mock(subject)
15
+ end
16
+
17
+ describe ".new" do
18
+ it "does not undefine object_id" do
19
+ blank_slate = DoubleDefinitionCreateBlankSlate.new(double_definition_create)
20
+ expect(blank_slate.object_id.class).to eq Fixnum
21
+ end
22
+
23
+ context "without block" do
24
+ before do
25
+ @blank_slate = DoubleDefinitionCreateBlankSlate.new(double_definition_create)
26
+ end
27
+
28
+ it "clears out all methods from proxy" do
29
+ expect(stub(subject).i_should_be_a_double).to be_instance_of(DoubleDefinition)
30
+ end
31
+ end
32
+
33
+ context "when passed a block" do
34
+ context "when the block has an arity of 1" do
35
+ attr_reader :passed_in_argument
36
+ before do
37
+ passed_in_argument = nil
38
+ stub(subject) do |b|
39
+ passed_in_argument = b
40
+ b.foobar(1, 2) {:one_two}
41
+ b.foobar(1) {:one}
42
+ b.foobar.with_any_args {:default}
43
+ b.baz() {:baz_result}
44
+ end
45
+ @passed_in_argument = passed_in_argument
46
+ end
47
+
48
+ it "creates double_injections" do
49
+ expect(subject.foobar(1, 2)).to eq :one_two
50
+ expect(subject.foobar(1)).to eq :one
51
+ expect(subject.foobar(:something)).to eq :default
52
+ expect(subject.baz).to eq :baz_result
53
+ end
54
+
55
+ it "passes the self into the block" do
56
+ expect(passed_in_argument.__double_definition_create__).to be_instance_of(
57
+ ::RR::DoubleDefinitions::DoubleDefinitionCreate
58
+ )
59
+ end
60
+ end
61
+
62
+ context "when the block has an arity of 0" do
63
+ attr_reader :self_value
64
+ before do
65
+ self_value = nil
66
+ stub(subject) do ||
67
+ self_value = self
68
+ foobar(1, 2) {:one_two}
69
+ foobar(1) {:one}
70
+ foobar.with_any_args {:default}
71
+ baz() {:baz_result}
72
+ end
73
+ @self_value = self_value
74
+ end
75
+
76
+ it "creates double_injections" do
77
+ expect(subject.foobar(1, 2)).to eq :one_two
78
+ expect(subject.foobar(1)).to eq :one
79
+ expect(subject.foobar(:something)).to eq :default
80
+ expect(subject.baz).to eq :baz_result
81
+ end
82
+
83
+ it "evaluates the block with the context of self" do
84
+ expect(self_value.__double_definition_create__).to be_instance_of(
85
+ ::RR::DoubleDefinitions::DoubleDefinitionCreate
86
+ )
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,446 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
2
+
3
+ module RR
4
+ module DoubleDefinitions
5
+ describe DoubleDefinitionCreate do
6
+ subject { Object.new }
7
+
8
+ attr_reader :double_definition_create, :strategy_method_name
9
+
10
+ include_examples "Swapped Space"
11
+
12
+ before(:each) do
13
+ @double_definition_create = DoubleDefinitionCreate.new
14
+ end
15
+
16
+ describe "#root_subject" do
17
+ it "returns #subject" do
18
+ double_definition_create.stub(subject).foobar
19
+ expect(double_definition_create.root_subject).to eq subject
20
+ end
21
+ end
22
+
23
+ describe "StrategySetupMethods" do
24
+ describe "normal strategy definitions" do
25
+ def call_strategy(*args, &block)
26
+ double_definition_create.__send__(strategy_method_name, *args, &block)
27
+ end
28
+
29
+ describe "#mock" do
30
+ before do
31
+ @strategy_method_name = :mock
32
+ end
33
+
34
+ context "when passing no args" do
35
+ it "returns self" do
36
+ expect(call_strategy).to equal double_definition_create
37
+ end
38
+ end
39
+
40
+ context "when passed a subject and a method_name argument" do
41
+ it "creates a mock Double for method" do
42
+ double_definition = double_definition_create.mock(subject, :foobar).returns {:baz}
43
+ expect(double_definition.times_matcher).to eq RR::TimesCalledMatchers::IntegerMatcher.new(1)
44
+ expect(double_definition.argument_expectation.class).to eq RR::Expectations::ArgumentEqualityExpectation
45
+ expect(double_definition.argument_expectation.expected_arguments).to eq []
46
+ expect(subject.foobar).to eq :baz
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "#stub" do
52
+ before do
53
+ @strategy_method_name = :stub
54
+ end
55
+
56
+ context "when passing no args" do
57
+ it "returns self" do
58
+ expect(call_strategy).to equal double_definition_create
59
+ end
60
+ end
61
+
62
+ context "when passed subject and a method_name argument" do
63
+ it "creates a stub Double for method when passed a method_name argument" do
64
+ double_definition = double_definition_create.stub(subject, :foobar).returns {:baz}
65
+ expect(double_definition.times_matcher).to eq RR::TimesCalledMatchers::AnyTimesMatcher.new
66
+ expect(double_definition.argument_expectation.class).to eq RR::Expectations::AnyArgumentExpectation
67
+ expect(subject.foobar).to eq :baz
68
+ end
69
+ end
70
+ end
71
+
72
+ describe "#dont_allow" do
73
+ before do
74
+ @strategy_method_name = :dont_allow
75
+ end
76
+
77
+ context "when passing no args" do
78
+ it "returns self" do
79
+ expect(call_strategy).to equal double_definition_create
80
+ end
81
+ end
82
+
83
+ context "when passed a subject and a method_name argument_expectation" do
84
+ it "creates a mock Double for method" do
85
+ double_definition = double_definition_create.dont_allow(subject, :foobar)
86
+ expect(double_definition.times_matcher).to eq RR::TimesCalledMatchers::NeverMatcher.new
87
+ expect(double_definition.argument_expectation.class).to eq RR::Expectations::AnyArgumentExpectation
88
+
89
+ expect {
90
+ subject.foobar
91
+ }.to raise_error(RR::Errors::TimesCalledError)
92
+ RR.reset
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ describe "! strategy definitions" do
99
+ attr_reader :strategy_method_name
100
+ def call_strategy(*args, &definition_eval_block)
101
+ double_definition_create.__send__(strategy_method_name, *args, &definition_eval_block)
102
+ end
103
+
104
+ describe "#mock!" do
105
+ before do
106
+ @strategy_method_name = :mock!
107
+ end
108
+
109
+ context "when passed a method_name argument" do
110
+ it "sets #verification_strategy to Mock" do
111
+ double_definition_create.mock!(:foobar)
112
+ expect(double_definition_create.verification_strategy.class).to eq Strategies::Verification::Mock
113
+ expect { RR.verify }.to raise_error(::RR::Errors::TimesCalledError)
114
+ end
115
+ end
116
+ end
117
+
118
+ describe "#stub!" do
119
+ before do
120
+ @strategy_method_name = :stub!
121
+ end
122
+
123
+ context "when passed a method_name argument" do
124
+ it "sets #verification_strategy to Stub" do
125
+ double_definition_create.stub!(:foobar)
126
+ expect(double_definition_create.verification_strategy.class).to eq Strategies::Verification::Stub
127
+ end
128
+ end
129
+ end
130
+
131
+ describe "#dont_allow!" do
132
+ before do
133
+ @strategy_method_name = :dont_allow!
134
+ end
135
+
136
+ context "when passed a method_name argument" do
137
+ it "sets #verification_strategy to DontAllow" do
138
+ double_definition_create.dont_allow!(:foobar)
139
+ expect(double_definition_create.verification_strategy.class).to eq Strategies::Verification::DontAllow
140
+ end
141
+ end
142
+ end
143
+ end
144
+
145
+ describe "#stub.proxy" do
146
+ before do
147
+ class << subject
148
+ def foobar(*args)
149
+ :original_foobar
150
+ end
151
+ end
152
+ end
153
+
154
+ context "when passed a method_name argument" do
155
+ it "creates a proxy Double for method" do
156
+ double_definition = double_definition_create.stub.proxy(subject, :foobar).after_call {:baz}
157
+ expect(double_definition.times_matcher).to eq RR::TimesCalledMatchers::AnyTimesMatcher.new
158
+ expect(double_definition.argument_expectation.class).to eq RR::Expectations::AnyArgumentExpectation
159
+ expect(subject.foobar).to eq :baz
160
+ end
161
+ end
162
+ end
163
+
164
+ describe "#instance_of" do
165
+ context "when not passed a class" do
166
+ it "raises an ArgumentError" do
167
+ expect {
168
+ double_definition_create.instance_of(Object.new).foobar
169
+ }.to raise_error(ArgumentError, "instance_of only accepts class objects")
170
+ end
171
+ end
172
+
173
+ context "when passed a method_name argument" do
174
+ it "creates a proxy Double for method" do
175
+ klass = Class.new
176
+ double_definition = double_definition_create.stub.instance_of(klass, :foobar).returns {:baz}
177
+ expect(double_definition.times_matcher).to eq RR::TimesCalledMatchers::AnyTimesMatcher.new
178
+ expect(double_definition.argument_expectation.class).to eq RR::Expectations::AnyArgumentExpectation
179
+ expect(klass.new.foobar).to eq :baz
180
+ end
181
+ end
182
+ end
183
+
184
+ describe "#instance_of.mock" do
185
+ before do
186
+ @klass = Class.new
187
+ end
188
+
189
+ # context "when passed no arguments" do
190
+ # it "returns a DoubleDefinitiondouble_definition_create" do
191
+ # expect(instance_of.instance_of).to be_instance_of(DoubleDefinitionCreate)
192
+ # end
193
+ # end
194
+
195
+ context "when passed a method_name argument" do
196
+ it "creates a instance_of Double for method" do
197
+ double_definition = instance_of.mock(@klass, :foobar)
198
+ double_definition.with(1, 2) {:baz}
199
+ expect(double_definition.times_matcher).to eq RR::TimesCalledMatchers::IntegerMatcher.new(1)
200
+ expect(double_definition.argument_expectation.class).to eq RR::Expectations::ArgumentEqualityExpectation
201
+ expect(double_definition.argument_expectation.expected_arguments).to eq [1, 2]
202
+
203
+ expect(@klass.new.foobar(1, 2)).to eq :baz
204
+ end
205
+ end
206
+ end
207
+ end
208
+
209
+ describe "StrategyExecutionMethods" do
210
+ describe "#create" do
211
+ context "when #verification_strategy is a Mock" do
212
+ context "when #implementation_strategy is a Reimplementation" do
213
+ before do
214
+ double_definition_create.mock(subject)
215
+ end
216
+
217
+ it "sets expectation on the #subject that it will be sent the method_name once with the passed-in arguments" do
218
+ mock(subject).foobar(1, 2)
219
+ subject.foobar(1, 2)
220
+ expect { subject.foobar(1, 2) }.to raise_error(RR::Errors::TimesCalledError)
221
+ expect { RR.verify }.to raise_error(RR::Errors::TimesCalledError)
222
+ end
223
+
224
+ describe "#subject.method_name being called" do
225
+ it "returns the return value of the Double#returns block" do
226
+ double_definition_create.call(:foobar, 1, 2) {:baz}
227
+ expect(subject.foobar(1, 2)).to eq :baz
228
+ end
229
+ end
230
+ end
231
+
232
+ context "when #implementation_strategy is a Proxy" do
233
+ before do
234
+ double_definition_create.mock
235
+ double_definition_create.proxy(subject)
236
+ end
237
+
238
+ it "sets expectation on the #subject that it will be sent the method_name once with the passed-in arguments" do
239
+ def subject.foobar(*args)
240
+ :baz
241
+ end
242
+ mock(subject).foobar(1, 2)
243
+
244
+ subject.foobar(1, 2)
245
+ expect { subject.foobar(1, 2) }.to raise_error(RR::Errors::TimesCalledError)
246
+ expect { RR.verify }.to raise_error(RR::Errors::TimesCalledError)
247
+ end
248
+
249
+ describe "#subject.method_name being called" do
250
+ it "calls the original method" do
251
+ original_method_called = false
252
+ (class << subject; self; end).class_eval do
253
+ define_method(:foobar) do |*args|
254
+ original_method_called = true
255
+ end
256
+ end
257
+ double_definition_create.call(:foobar, 1, 2)
258
+ subject.foobar(1, 2)
259
+ expect(original_method_called).to be_true
260
+ end
261
+
262
+ context "when not passed a block" do
263
+ it "returns the value of the original method" do
264
+ def subject.foobar(*args)
265
+ :baz;
266
+ end
267
+ double_definition_create.call(:foobar, 1, 2)
268
+ expect(subject.foobar(1, 2)).to eq :baz
269
+ end
270
+ end
271
+
272
+ context "when passed a block" do
273
+ attr_reader :real_value
274
+ before do
275
+ @real_value = real_value = Object.new
276
+ (class << subject; self; end).class_eval do
277
+ define_method(:foobar) {|arg1, arg2| real_value}
278
+ end
279
+ end
280
+
281
+ it "calls the block with the return value of the original method" do
282
+ double_definition_create.call(:foobar, 1, 2) do |value|
283
+ mock(value).a_method {99}
284
+ value
285
+ end
286
+ subject.foobar(1, 2)
287
+ expect(real_value.a_method).to eq 99
288
+ end
289
+
290
+ it "returns the return value of the block" do
291
+ double_definition_create.call(:foobar, 1, 2) do |value|
292
+ :something_else
293
+ end
294
+ expect(subject.foobar(1, 2)).to eq :something_else
295
+ end
296
+ end
297
+ end
298
+ end
299
+ end
300
+
301
+ context "when #verification_strategy is a Stub" do
302
+ context "when #implementation_strategy is a Reimplementation" do
303
+ before do
304
+ double_definition_create.stub(subject)
305
+ end
306
+
307
+ context "when not passed a block" do
308
+ it "returns nil" do
309
+ double_definition_create.call(:foobar)
310
+ expect(subject.foobar).to be_nil
311
+ end
312
+ end
313
+
314
+ context "when passed a block" do
315
+ describe "#subject.method_name being called" do
316
+ it "returns the return value of the block" do
317
+ double_definition_create.call(:foobar) {:baz}
318
+ expect(subject.foobar).to eq :baz
319
+ end
320
+ end
321
+ end
322
+
323
+ context "when not passed args" do
324
+ describe "#subject.method_name being called with any arguments" do
325
+ it "invokes the implementation of the Stub" do
326
+ double_definition_create.call(:foobar) {:baz}
327
+ expect(subject.foobar(1, 2)).to eq :baz
328
+ expect(subject.foobar()).to eq :baz
329
+ expect(subject.foobar([])).to eq :baz
330
+ end
331
+ end
332
+ end
333
+
334
+ context "when passed args" do
335
+ describe "#subject.method_name being called with the passed-in arguments" do
336
+ it "invokes the implementation of the Stub" do
337
+ double_definition_create.call(:foobar, 1, 2) {:baz}
338
+ expect(subject.foobar(1, 2)).to eq :baz
339
+ end
340
+ end
341
+
342
+ describe "#subject.method_name being called with different arguments" do
343
+ it "raises a DoubleNotFoundError" do
344
+ double_definition_create.call(:foobar, 1, 2) {:baz}
345
+ expect {
346
+ subject.foobar
347
+ }.to raise_error(RR::Errors::DoubleNotFoundError)
348
+ end
349
+ end
350
+ end
351
+ end
352
+
353
+ context "when #implementation_strategy is a Proxy" do
354
+ before do
355
+ def subject.foobar(*args)
356
+ :original_return_value
357
+ end
358
+ double_definition_create.stub
359
+ double_definition_create.proxy(subject)
360
+ end
361
+
362
+ context "when not passed a block" do
363
+ describe "#subject.method_name being called" do
364
+ it "invokes the original implementanion" do
365
+ double_definition_create.call(:foobar)
366
+ expect(subject.foobar).to eq :original_return_value
367
+ end
368
+ end
369
+ end
370
+
371
+ context "when passed a block" do
372
+ describe "#subject.method_name being called" do
373
+ it "invokes the original implementanion and invokes the block with the return value of the original implementanion" do
374
+ passed_in_value = nil
375
+ double_definition_create.call(:foobar) do |original_return_value|
376
+ passed_in_value = original_return_value
377
+ end
378
+ subject.foobar
379
+ expect(passed_in_value).to eq :original_return_value
380
+ end
381
+
382
+ it "returns the return value of the block" do
383
+ double_definition_create.call(:foobar) do |original_return_value|
384
+ :new_return_value
385
+ end
386
+ expect(subject.foobar).to eq :new_return_value
387
+ end
388
+ end
389
+ end
390
+
391
+ context "when passed args" do
392
+ describe "#subject.method_name being called with the passed-in arguments" do
393
+ it "invokes the implementation of the Stub" do
394
+ double_definition_create.call(:foobar, 1, 2) {:baz}
395
+ expect(subject.foobar(1, 2)).to eq :baz
396
+ end
397
+ end
398
+
399
+ describe "#subject.method_name being called with different arguments" do
400
+ it "raises a DoubleNotFoundError" do
401
+ double_definition_create.call(:foobar, 1, 2) {:baz}
402
+ expect {
403
+ subject.foobar
404
+ }.to raise_error(RR::Errors::DoubleNotFoundError)
405
+ end
406
+ end
407
+ end
408
+ end
409
+ end
410
+
411
+ context "when #verification_strategy is a DontAllow" do
412
+ before do
413
+ double_definition_create.dont_allow(subject)
414
+ end
415
+
416
+ context "when not passed args" do
417
+ describe "#subject.method_name being called with any arguments" do
418
+ it "raises a TimesCalledError" do
419
+ double_definition_create.call(:foobar)
420
+ expect { subject.foobar }.to raise_error(RR::Errors::TimesCalledError)
421
+ expect { subject.foobar(1, 2) }.to raise_error(RR::Errors::TimesCalledError)
422
+ end
423
+ end
424
+ end
425
+
426
+ context "when passed args" do
427
+ describe "#subject.method_name being called with the passed-in arguments" do
428
+ it "raises a TimesCalledError" do
429
+ double_definition_create.call(:foobar, 1, 2)
430
+ expect { subject.foobar(1, 2) }.to raise_error(RR::Errors::TimesCalledError)
431
+ end
432
+ end
433
+
434
+ describe "#subject.method_name being called with different arguments" do
435
+ it "raises a DoubleNotFoundError" do
436
+ double_definition_create.call(:foobar, 1, 2)
437
+ expect { subject.foobar() }.to raise_error(RR::Errors::DoubleNotFoundError)
438
+ end
439
+ end
440
+ end
441
+ end
442
+ end
443
+ end
444
+ end
445
+ end
446
+ end