flexmock 1.3.3 → 2.0.0.rc1
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 +4 -4
- data/.autotest +3 -0
- data/.gitignore +14 -0
- data/.togglerc +7 -0
- data/.travis.yml +5 -0
- data/.yardopts +2 -0
- data/CHANGES +11 -0
- data/Gemfile +1 -4
- data/README.md +39 -11
- data/Rakefile +6 -217
- data/doc/examples/rspec_examples_spec.rb +244 -0
- data/doc/examples/test_unit_examples_test.rb +240 -0
- data/doc/jamis.rb +591 -0
- data/flexmock.gemspec +33 -0
- data/lib/flexmock.rb +0 -1
- data/lib/flexmock/composite_expectation.rb +1 -1
- data/lib/flexmock/core.rb +3 -7
- data/lib/flexmock/core_class_methods.rb +5 -1
- data/lib/flexmock/default_framework_adapter.rb +2 -2
- data/lib/flexmock/expectation.rb +29 -3
- data/lib/flexmock/expectation_director.rb +1 -1
- data/lib/flexmock/minitest.rb +13 -0
- data/lib/flexmock/minitest_extensions.rb +26 -0
- data/lib/flexmock/minitest_integration.rb +111 -0
- data/lib/flexmock/mock_container.rb +1 -2
- data/lib/flexmock/partial_mock.rb +61 -104
- data/lib/flexmock/recorder.rb +1 -2
- data/lib/flexmock/rspec.rb +6 -3
- data/lib/flexmock/test_unit_integration.rb +14 -0
- data/lib/flexmock/validators.rb +5 -4
- data/lib/flexmock/version.rb +1 -9
- data/rakelib/metrics.rake +40 -0
- data/rakelib/preview.rake +4 -0
- data/rakelib/tags.rake +18 -0
- data/todo.txt +20 -0
- metadata +61 -86
- data/Gemfile.lock +0 -20
- data/doc/examples/rspec_examples_spec.rdoc +0 -245
- data/doc/examples/test_unit_examples_test.rdoc +0 -241
- data/test/aliasing_test.rb +0 -66
- data/test/assert_spy_called_test.rb +0 -119
- data/test/base_class_test.rb +0 -71
- data/test/based_partials_test.rb +0 -51
- data/test/container_methods_test.rb +0 -118
- data/test/default_framework_adapter_test.rb +0 -38
- data/test/demeter_mocking_test.rb +0 -191
- data/test/deprecated_methods_test.rb +0 -225
- data/test/examples_from_readme_test.rb +0 -157
- data/test/expectation_description_test.rb +0 -80
- data/test/extended_should_receive_test.rb +0 -69
- data/test/flexmodel_test.rb +0 -54
- data/test/mock_builder_test.rb +0 -68
- data/test/naming_test.rb +0 -84
- data/test/new_instances_test.rb +0 -215
- data/test/object_extensions_test.rb +0 -25
- data/test/partial_mock_test.rb +0 -458
- data/test/record_mode_test.rb +0 -158
- data/test/redirect_error.rb +0 -16
- data/test/rspec_integration/integration_spec.rb +0 -56
- data/test/rspec_integration/spy_example_spec.rb +0 -207
- data/test/samples_test.rb +0 -283
- data/test/should_ignore_missing_test.rb +0 -84
- data/test/should_receive_test.rb +0 -1155
- data/test/spys_test.rb +0 -215
- data/test/symbol_extensions_test.rb +0 -8
- data/test/test_class_extensions.rb +0 -34
- data/test/test_setup.rb +0 -92
- data/test/test_unit_integration/auto_test_unit_test.rb +0 -42
- data/test/test_unit_integration/minitest_teardown_test.rb +0 -14
- data/test/tu_integration_test.rb +0 -99
- data/test/undefined_test.rb +0 -87
data/test/new_instances_test.rb
DELETED
@@ -1,215 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
#---
|
4
|
-
# Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
|
5
|
-
# All rights reserved.
|
6
|
-
|
7
|
-
# Permission is granted for use, copying, modification, distribution,
|
8
|
-
# and distribution of modified versions of this work as long as the
|
9
|
-
# above copyright notice is included.
|
10
|
-
#+++
|
11
|
-
|
12
|
-
require 'test/test_setup'
|
13
|
-
|
14
|
-
class TestNewInstances < Test::Unit::TestCase
|
15
|
-
include FlexMock::TestCase
|
16
|
-
include FlexMock::RedirectError
|
17
|
-
|
18
|
-
class Dog
|
19
|
-
def bark
|
20
|
-
:woof
|
21
|
-
end
|
22
|
-
def wag
|
23
|
-
:tail
|
24
|
-
end
|
25
|
-
def self.make
|
26
|
-
new
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
class Cat
|
31
|
-
attr_reader :name
|
32
|
-
def initialize(name, &block)
|
33
|
-
@name = name
|
34
|
-
block.call(self) if block_given?
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
class Connection
|
39
|
-
def initialize(*args)
|
40
|
-
yield(self) if block_given?
|
41
|
-
end
|
42
|
-
def send(args)
|
43
|
-
post(args)
|
44
|
-
end
|
45
|
-
def post(args)
|
46
|
-
:unstubbed
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_new_instances_allows_stubbing_of_existing_methods
|
51
|
-
flexstub(Dog).new_instances do |obj|
|
52
|
-
obj.should_receive(:bark).and_return(:whimper)
|
53
|
-
end
|
54
|
-
m = Dog.new
|
55
|
-
assert_equal :whimper, m.bark
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_new_instances_stubs_still_have_existing_methods
|
59
|
-
flexstub(Dog).new_instances do |obj|
|
60
|
-
obj.should_receive(:bark).and_return(:whimper)
|
61
|
-
end
|
62
|
-
m = Dog.new
|
63
|
-
assert_equal :tail, m.wag
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_new_instances_will_pass_args_to_new
|
67
|
-
flexstub(Cat).new_instances do |obj|
|
68
|
-
obj.should_receive(:meow).and_return(:scratch)
|
69
|
-
end
|
70
|
-
x = :not_called
|
71
|
-
m = Cat.new("Fido") { x = :called }
|
72
|
-
assert_equal :scratch, m.meow
|
73
|
-
assert_equal "Fido", m.name
|
74
|
-
assert_equal :called, x
|
75
|
-
end
|
76
|
-
|
77
|
-
# Some versions of the software had problems invoking the block after a
|
78
|
-
# second stubbing.
|
79
|
-
def test_new_gets_block_after_restubbing
|
80
|
-
flexstub(Cat).new_instances { }
|
81
|
-
x = :not_called
|
82
|
-
m = Cat.new("Fido") { x = :called }
|
83
|
-
assert_equal :called, x
|
84
|
-
flexmock_teardown
|
85
|
-
|
86
|
-
flexstub(Cat).new_instances { }
|
87
|
-
x = :not_called
|
88
|
-
m = Cat.new("Fido") { x = :called }
|
89
|
-
assert_equal :called, x
|
90
|
-
end
|
91
|
-
|
92
|
-
def test_new_instances_stub_verification_happens_on_teardown
|
93
|
-
flexstub(Dog).new_instances do |obj|
|
94
|
-
obj.should_receive(:bark).once.and_return(nil)
|
95
|
-
end
|
96
|
-
|
97
|
-
Dog.new
|
98
|
-
ex = assert_raise(assertion_failed_error) { flexmock_teardown }
|
99
|
-
assert_match(/method 'bark\(.*\)' called incorrect number of times/i, ex.message)
|
100
|
-
end
|
101
|
-
|
102
|
-
def test_new_instances_reports_error_on_non_classes
|
103
|
-
ex = assert_raise(ArgumentError) {
|
104
|
-
flexstub(Dog.new).new_instances do |obj|
|
105
|
-
obj.should_receive(:hi)
|
106
|
-
end
|
107
|
-
}
|
108
|
-
assert_match(/Class/, ex.message)
|
109
|
-
assert_match(/new_instances/, ex.message)
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_does_not_by_default_stub_objects_created_with_allocate
|
113
|
-
flexstub(Dog).new_instances do |obj|
|
114
|
-
obj.should_receive(:bark).and_return(:whimper)
|
115
|
-
end
|
116
|
-
m = Dog.allocate
|
117
|
-
assert_equal :woof, m.bark
|
118
|
-
end
|
119
|
-
|
120
|
-
if RUBY_VERSION >= "1.9"
|
121
|
-
def test_explicitly_mocking_allocation_in_new_instances_fails_in_ruby_19
|
122
|
-
assert_raise FlexMock::UsageError do
|
123
|
-
flexstub(Dog).new_instances(:allocate) do |obj|
|
124
|
-
obj.should_receive(:bark).and_return(:whimper)
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
128
|
-
else
|
129
|
-
def test_can_explicitly_stub_objects_created_with_allocate
|
130
|
-
flexstub(Dog).new_instances(:allocate) do |obj|
|
131
|
-
obj.should_receive(:bark).and_return(:whimper)
|
132
|
-
end
|
133
|
-
m = Dog.allocate
|
134
|
-
assert_equal :whimper, m.bark
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
def test_can_stub_objects_created_with_arbitrary_class_methods
|
139
|
-
flexstub(Dog).new_instances(:make) do |obj|
|
140
|
-
obj.should_receive(:bark).and_return(:whimper)
|
141
|
-
end
|
142
|
-
assert_equal :whimper, Dog.make.bark
|
143
|
-
end
|
144
|
-
|
145
|
-
def test_stubbing_arbitrary_class_methods_leaves_new_alone
|
146
|
-
flexstub(Dog).new_instances(:make) do |obj|
|
147
|
-
obj.should_receive(:bark).and_return(:whimper)
|
148
|
-
end
|
149
|
-
assert_equal :woof, Dog.new.bark
|
150
|
-
end
|
151
|
-
|
152
|
-
def test_stubbing_new_and_allocate_doesnt_double_stub_objects_on_new
|
153
|
-
counter = 0
|
154
|
-
flexstub(Dog).new_instances do |obj|
|
155
|
-
counter += 1
|
156
|
-
end
|
157
|
-
Dog.new
|
158
|
-
assert_equal 1, counter
|
159
|
-
end
|
160
|
-
|
161
|
-
# Current behavior does not install stubs into the block passed to new.
|
162
|
-
# This is rather difficult to achieve, although it would be nice. For the
|
163
|
-
# moment, we assure that they are not stubbed, but I am willing to change
|
164
|
-
# this in the future.
|
165
|
-
def test_blocks_on_new_do_not_have_stubs_installed
|
166
|
-
flexstub(Connection).new_instances do |new_con|
|
167
|
-
new_con.should_receive(:post).and_return {
|
168
|
-
:stubbed
|
169
|
-
}
|
170
|
-
end
|
171
|
-
block_run = false
|
172
|
-
Connection.new do |c|
|
173
|
-
assert_equal :unstubbed, c.send("hi")
|
174
|
-
block_run = true
|
175
|
-
end
|
176
|
-
assert block_run
|
177
|
-
end
|
178
|
-
|
179
|
-
def test_new_instances_accept_chained_expectations
|
180
|
-
flexmock(Dog).new_instances.
|
181
|
-
should_receive(:growl).and_return(:grr).
|
182
|
-
should_receive(:roll_over).and_return(:flip)
|
183
|
-
assert_equal :grr, Dog.new.growl
|
184
|
-
assert_equal :flip, Dog.new.roll_over
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_fancy_use_of_chained_should_received
|
188
|
-
flexmock(Dog).new_instances.should_receive(:woof => :grrr)
|
189
|
-
assert_equal :grrr, Dog.new.woof
|
190
|
-
end
|
191
|
-
|
192
|
-
def test_writable_accessors
|
193
|
-
flexmock(Dog).new_instances.should_receive(:name=).with("fido")
|
194
|
-
dog = Dog.new
|
195
|
-
dog.name = 'fido'
|
196
|
-
end
|
197
|
-
|
198
|
-
def test_ordering_can_be_specified
|
199
|
-
dog = Dog.new
|
200
|
-
flexmock(dog).should_receive(:bark).once.ordered
|
201
|
-
flexmock(dog).should_receive(:bite).once.ordered
|
202
|
-
dog.bark
|
203
|
-
dog.bite
|
204
|
-
end
|
205
|
-
|
206
|
-
def test_ordering_can_be_specified_in_groups
|
207
|
-
dog = Dog.new
|
208
|
-
flexmock(dog).should_receive(:wag).once.ordered(:safe)
|
209
|
-
flexmock(dog).should_receive(:bark).once.ordered(:danger)
|
210
|
-
flexmock(dog).should_receive(:bite).once.ordered(:danger)
|
211
|
-
dog.wag
|
212
|
-
dog.bite
|
213
|
-
dog.bark
|
214
|
-
end
|
215
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'test/test_setup'
|
4
|
-
require 'flexmock/object_extensions'
|
5
|
-
|
6
|
-
class ObjectExtensionsTest < Test::Unit::TestCase
|
7
|
-
def setup
|
8
|
-
@obj = Object.new
|
9
|
-
def @obj.smethod
|
10
|
-
:ok
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_undefined_methods_are_not_singletons
|
15
|
-
assert ! @obj.flexmock_singleton_defined?(:xyzzy)
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_normal_methods_are_not_singletons
|
19
|
-
assert ! @obj.flexmock_singleton_defined?(:to_s)
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_singleton_methods_are_singletons
|
23
|
-
assert @obj.flexmock_singleton_defined?(:smethod)
|
24
|
-
end
|
25
|
-
end
|
data/test/partial_mock_test.rb
DELETED
@@ -1,458 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
#---
|
4
|
-
# Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
|
5
|
-
# All rights reserved.
|
6
|
-
|
7
|
-
# Permission is granted for use, copying, modification, distribution,
|
8
|
-
# and distribution of modified versions of this work as long as the
|
9
|
-
# above copyright notice is included.
|
10
|
-
#+++
|
11
|
-
|
12
|
-
require 'test/test_setup'
|
13
|
-
|
14
|
-
class TestStubbing < Test::Unit::TestCase
|
15
|
-
include FlexMock::TestCase
|
16
|
-
|
17
|
-
class Dog
|
18
|
-
def bark
|
19
|
-
:woof
|
20
|
-
end
|
21
|
-
def Dog.create
|
22
|
-
:new_dog
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
class DogPlus < Dog
|
27
|
-
def should_receive
|
28
|
-
:dog_should
|
29
|
-
end
|
30
|
-
def new_instances
|
31
|
-
:dog_new
|
32
|
-
end
|
33
|
-
def by_default
|
34
|
-
:dog_by_default
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_attempting_to_partially_mock_existing_mock_is_noop
|
39
|
-
m = flexmock("A")
|
40
|
-
flexmock(m)
|
41
|
-
assert ! m.instance_variables.include?(:@flexmock_proxy.flexmock_as_name)
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_forcibly_removing_proxy_causes_failure
|
45
|
-
obj = Object.new
|
46
|
-
flexmock(obj)
|
47
|
-
obj.instance_eval { @flexmock_proxy = nil }
|
48
|
-
assert_raises(RuntimeError, /missing.*proxy/i) do
|
49
|
-
obj.should_receive(:hi).and_return(:stub_hi)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_stub_command_add_behavior_to_arbitrary_objects_via_flexmock
|
54
|
-
obj = Object.new
|
55
|
-
flexmock(obj).should_receive(:hi).and_return(:stub_hi)
|
56
|
-
assert_equal :stub_hi, obj.hi
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_stub_command_add_behavior_to_arbitrary_objects_post_flexmock
|
60
|
-
obj = Object.new
|
61
|
-
flexmock(obj)
|
62
|
-
obj.should_receive(:hi).and_return(:stub_hi)
|
63
|
-
assert_equal :stub_hi, obj.hi
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_stub_command_can_configure_via_block
|
67
|
-
obj = Object.new
|
68
|
-
flexmock(obj) do |m|
|
69
|
-
m.should_receive(:hi).once.and_return(:stub_hi)
|
70
|
-
end
|
71
|
-
assert_equal :stub_hi, obj.hi
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_stubbed_methods_can_take_blocks
|
75
|
-
obj = Object.new
|
76
|
-
flexmock(obj).should_receive(:with_block).once.with(Proc).
|
77
|
-
and_return { |block| block.call }
|
78
|
-
assert_equal :block, obj.with_block { :block }
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_multiple_stubs_on_the_same_object_reuse_the_same_partial_mock
|
82
|
-
obj = Object.new
|
83
|
-
a = flexmock(obj)
|
84
|
-
b = flexmock(obj)
|
85
|
-
assert_equal a.object_id, b.object_id
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_stubbed_methods_can_invoke_original_behavior_directly
|
89
|
-
dog = Dog.new
|
90
|
-
flexmock(dog).should_receive(:bark).pass_thru.once
|
91
|
-
assert_equal :woof, dog.bark
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_stubbed_methods_can_invoke_original_behavior_with_modification
|
95
|
-
dog = Dog.new
|
96
|
-
flexmock(dog).should_receive(:bark).pass_thru { |result| result.to_s.upcase }.once
|
97
|
-
assert_equal "WOOF", dog.bark
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_stubbed_methods_returning_partial_mocks
|
101
|
-
flexmock(Dog).should_receive(:new).pass_thru { |dog|
|
102
|
-
flexmock(dog, :beg => "Please")
|
103
|
-
}.once
|
104
|
-
dog = Dog.new
|
105
|
-
assert_equal "Please", dog.beg
|
106
|
-
assert_equal :woof, dog.bark
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_multiple_methods_can_be_stubbed
|
110
|
-
dog = Dog.new
|
111
|
-
flexmock(dog).should_receive(:bark).and_return(:grrrr)
|
112
|
-
flexmock(dog).should_receive(:wag).and_return(:happy)
|
113
|
-
assert_equal :grrrr, dog.bark
|
114
|
-
assert_equal :happy, dog.wag
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_original_behavior_can_be_restored
|
118
|
-
dog = Dog.new
|
119
|
-
partial_mock = flexmock(dog)
|
120
|
-
partial_mock.should_receive(:bark).once.and_return(:growl)
|
121
|
-
assert_equal :growl, dog.bark
|
122
|
-
partial_mock.flexmock_teardown
|
123
|
-
assert_equal :woof, dog.bark
|
124
|
-
assert_equal nil, dog.instance_variable_get("@flexmock_proxy")
|
125
|
-
end
|
126
|
-
|
127
|
-
def test_original_missing_behavior_can_be_restored
|
128
|
-
obj = Object.new
|
129
|
-
partial_mock = flexmock(obj)
|
130
|
-
partial_mock.should_receive(:hi).once.and_return(:ok)
|
131
|
-
assert_equal :ok, obj.hi
|
132
|
-
partial_mock.flexmock_teardown
|
133
|
-
assert_raise(NoMethodError) { obj.hi }
|
134
|
-
end
|
135
|
-
|
136
|
-
def test_multiple_stubs_on_single_method_can_be_restored_missing_method
|
137
|
-
obj = Object.new
|
138
|
-
partial_mock = flexmock(obj)
|
139
|
-
partial_mock.should_receive(:hi).with(1).once.and_return(:ok)
|
140
|
-
partial_mock.should_receive(:hi).with(2).once.and_return(:ok)
|
141
|
-
assert_equal :ok, obj.hi(1)
|
142
|
-
assert_equal :ok, obj.hi(2)
|
143
|
-
partial_mock.flexmock_teardown
|
144
|
-
assert_raise(NoMethodError) { obj.hi }
|
145
|
-
end
|
146
|
-
|
147
|
-
def test_original_behavior_is_restored_when_multiple_methods_are_mocked
|
148
|
-
dog = Dog.new
|
149
|
-
flexmock(dog).should_receive(:bark).and_return(:grrrr)
|
150
|
-
flexmock(dog).should_receive(:wag).and_return(:happy)
|
151
|
-
flexmock(dog).flexmock_teardown
|
152
|
-
assert_equal :woof, dog.bark
|
153
|
-
assert_raise(NoMethodError) { dog.wag }
|
154
|
-
end
|
155
|
-
|
156
|
-
def test_original_behavior_is_restored_on_class_objects
|
157
|
-
flexmock(Dog).should_receive(:create).once.and_return(:new_stub)
|
158
|
-
assert_equal :new_stub, Dog.create
|
159
|
-
flexmock(Dog).flexmock_teardown
|
160
|
-
assert_equal :new_dog, Dog.create
|
161
|
-
end
|
162
|
-
|
163
|
-
def test_original_behavior_is_restored_on_singleton_methods
|
164
|
-
obj = Object.new
|
165
|
-
def obj.hi() :hello end
|
166
|
-
flexmock(obj).should_receive(:hi).once.and_return(:hola)
|
167
|
-
|
168
|
-
assert_equal :hola, obj.hi
|
169
|
-
flexmock(obj).flexmock_teardown
|
170
|
-
assert_equal :hello, obj.hi
|
171
|
-
end
|
172
|
-
|
173
|
-
def test_original_behavior_is_restored_on_singleton_methods_with_multiple_stubs
|
174
|
-
obj = Object.new
|
175
|
-
def obj.hi(n) "hello#{n}" end
|
176
|
-
flexmock(obj).should_receive(:hi).with(1).once.and_return(:hola)
|
177
|
-
flexmock(obj).should_receive(:hi).with(2).once.and_return(:hola)
|
178
|
-
|
179
|
-
assert_equal :hola, obj.hi(1)
|
180
|
-
assert_equal :hola, obj.hi(2)
|
181
|
-
flexmock(obj).flexmock_teardown
|
182
|
-
assert_equal "hello3", obj.hi(3)
|
183
|
-
end
|
184
|
-
|
185
|
-
def test_original_behavior_is_restored_on_nonsingleton_methods_with_multiple_stubs
|
186
|
-
flexmock(Dir).should_receive(:chdir).with("xx").once.and_return(:ok1)
|
187
|
-
flexmock(Dir).should_receive(:chdir).with("yy").once.and_return(:ok2)
|
188
|
-
assert_equal :ok1, Dir.chdir("xx")
|
189
|
-
assert_equal :ok2, Dir.chdir("yy")
|
190
|
-
|
191
|
-
flexmock(Dir).flexmock_teardown
|
192
|
-
|
193
|
-
x = :not_called
|
194
|
-
Dir.chdir("test") do
|
195
|
-
assert_match %r{/test$}, Dir.pwd
|
196
|
-
x = :called
|
197
|
-
end
|
198
|
-
assert_equal :called, x
|
199
|
-
end
|
200
|
-
|
201
|
-
def test_stubbing_file_shouldnt_break_writing
|
202
|
-
flexmock(File).should_receive(:open).with("foo").once.and_return(:ok)
|
203
|
-
assert_equal :ok, File.open("foo")
|
204
|
-
flexmock(File).flexmock_teardown
|
205
|
-
|
206
|
-
File.open("dummy.txt", "w") do |out|
|
207
|
-
assert out.is_a?(IO)
|
208
|
-
out.puts "XYZ"
|
209
|
-
end
|
210
|
-
text = File.open("dummy.txt") { |f| f.read }
|
211
|
-
assert_equal "XYZ\n", text
|
212
|
-
ensure
|
213
|
-
FileUtils.rm_f("dummy.txt")
|
214
|
-
end
|
215
|
-
|
216
|
-
def test_original_behavior_is_restored_even_when_errors
|
217
|
-
flexmock(Dog).should_receive(:create).once.and_return(:mock)
|
218
|
-
begin
|
219
|
-
flexmock_teardown
|
220
|
-
rescue assertion_failed_error => _
|
221
|
-
nil
|
222
|
-
end
|
223
|
-
assert_equal :new_dog, Dog.create
|
224
|
-
|
225
|
-
# Now disable the mock so that it doesn't cause errors on normal
|
226
|
-
# test teardown
|
227
|
-
m = flexmock(Dog).flexmock_get
|
228
|
-
def m.flexmock_verify() end
|
229
|
-
end
|
230
|
-
|
231
|
-
def test_not_calling_stubbed_method_is_an_error
|
232
|
-
dog = Dog.new
|
233
|
-
flexmock(dog).should_receive(:bark).once
|
234
|
-
assert_raise(assertion_failed_error) {
|
235
|
-
flexmock(dog).flexmock_verify
|
236
|
-
}
|
237
|
-
dog.bark
|
238
|
-
end
|
239
|
-
|
240
|
-
def test_mock_is_verified_when_the_stub_is_verified
|
241
|
-
obj = Object.new
|
242
|
-
partial_mock = flexmock(obj)
|
243
|
-
partial_mock.should_receive(:hi).once.and_return(:ok)
|
244
|
-
assert_raise(assertion_failed_error) {
|
245
|
-
partial_mock.flexmock_verify
|
246
|
-
}
|
247
|
-
end
|
248
|
-
|
249
|
-
def test_stub_can_have_explicit_name
|
250
|
-
obj = Object.new
|
251
|
-
partial_mock = flexmock(obj, "Charlie")
|
252
|
-
assert_equal "Charlie", partial_mock.flexmock_get.flexmock_name
|
253
|
-
end
|
254
|
-
|
255
|
-
def test_unamed_stub_will_use_default_naming_convention
|
256
|
-
obj = Object.new
|
257
|
-
partial_mock = flexmock(obj)
|
258
|
-
assert_equal "flexmock(Object)", partial_mock.flexmock_get.flexmock_name
|
259
|
-
end
|
260
|
-
|
261
|
-
def test_partials_can_be_defined_in_a_block
|
262
|
-
dog = Dog.new
|
263
|
-
flexmock(dog) do |m|
|
264
|
-
m.should_receive(:bark).and_return(:growl)
|
265
|
-
end
|
266
|
-
assert_equal :growl, dog.bark
|
267
|
-
end
|
268
|
-
|
269
|
-
def test_partials_defining_block_return_real_obj_not_proxy
|
270
|
-
dog = flexmock(Dog.new) do |m|
|
271
|
-
m.should_receive(:bark).and_return(:growl)
|
272
|
-
end
|
273
|
-
assert_equal :growl, dog.bark
|
274
|
-
end
|
275
|
-
|
276
|
-
def test_partial_mocks_always_return_domain_object
|
277
|
-
dog = Dog.new
|
278
|
-
assert_equal dog, flexmock(dog)
|
279
|
-
assert_equal dog, flexmock(dog) { }
|
280
|
-
end
|
281
|
-
|
282
|
-
MOCK_METHOD_SUBSET = [
|
283
|
-
:should_receive, :new_instances,
|
284
|
-
:flexmock_get, :flexmock_teardown, :flexmock_verify,
|
285
|
-
]
|
286
|
-
|
287
|
-
def test_domain_objects_do_not_have_mock_methods
|
288
|
-
dog = Dog.new
|
289
|
-
MOCK_METHOD_SUBSET.each do |sym|
|
290
|
-
assert ! dog.respond_to?(sym), "should not have :#{sym} defined"
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
def test_partial_mocks_have_mock_methods
|
295
|
-
dog = Dog.new
|
296
|
-
flexmock(dog)
|
297
|
-
MOCK_METHOD_SUBSET.each do |sym|
|
298
|
-
assert dog.respond_to?(sym), "should have :#{sym} defined"
|
299
|
-
end
|
300
|
-
end
|
301
|
-
|
302
|
-
def test_partial_mocks_do_not_have_mock_methods_after_teardown
|
303
|
-
dog = Dog.new
|
304
|
-
flexmock(dog)
|
305
|
-
dog.flexmock_teardown
|
306
|
-
MOCK_METHOD_SUBSET.each do |sym|
|
307
|
-
assert ! dog.respond_to?(sym), "should not have :#{sym} defined"
|
308
|
-
end
|
309
|
-
end
|
310
|
-
|
311
|
-
# This test ensures that singleton? does not use the old methods(false)
|
312
|
-
# call that has fallen out of favor in Ruby 1.9. In multiple 1.9 releases
|
313
|
-
# Delegator#methods will not even accept the optional argument, making flexmock
|
314
|
-
# explode. Since there is a way to get singleton methods officially we might
|
315
|
-
# as well just do it, right?
|
316
|
-
class NoMethods
|
317
|
-
def methods(arg = true)
|
318
|
-
raise "Should not be called in the test lifecycle"
|
319
|
-
end
|
320
|
-
end
|
321
|
-
|
322
|
-
def xtest_object_methods_method_is_not_used_in_singleton_checks
|
323
|
-
obj = NoMethods.new
|
324
|
-
def obj.mock() :original end
|
325
|
-
assert_nothing_raised { flexmock(obj) }
|
326
|
-
end
|
327
|
-
|
328
|
-
def test_partial_mocks_with_mock_method_singleton_colision_have_original_defs_restored
|
329
|
-
dog = Dog.new
|
330
|
-
def dog.mock() :original end
|
331
|
-
flexmock(dog)
|
332
|
-
dog.flexmock_teardown
|
333
|
-
assert_equal :original, dog.mock
|
334
|
-
end
|
335
|
-
|
336
|
-
class MockColision
|
337
|
-
def mock
|
338
|
-
:original
|
339
|
-
end
|
340
|
-
end
|
341
|
-
|
342
|
-
def test_partial_mocks_with_mock_method_non_singleton_colision_have_original_defs_restored
|
343
|
-
mc = MockColision.new
|
344
|
-
flexmock(mc)
|
345
|
-
mc.flexmock_teardown
|
346
|
-
assert_equal :original, mc.mock
|
347
|
-
end
|
348
|
-
|
349
|
-
def test_safe_partial_mocks_do_not_support_mock_methods
|
350
|
-
dog = Dog.new
|
351
|
-
flexmock(:safe, dog) { }
|
352
|
-
MOCK_METHOD_SUBSET.each do |sym|
|
353
|
-
assert ! dog.respond_to?(sym), "should not have :#{sym} defined"
|
354
|
-
end
|
355
|
-
end
|
356
|
-
|
357
|
-
def test_safe_partial_mocks_require_block
|
358
|
-
dog = Dog.new
|
359
|
-
assert_raise(FlexMock::UsageError) { flexmock(:safe, dog) }
|
360
|
-
end
|
361
|
-
|
362
|
-
def test_safe_partial_mocks_are_actually_mocked
|
363
|
-
dog = flexmock(:safe, Dog.new) { |m| m.should_receive(:bark => :mocked) }
|
364
|
-
assert_equal :mocked, dog.bark
|
365
|
-
end
|
366
|
-
|
367
|
-
def test_should_receive_does_not_override_preexisting_def
|
368
|
-
dog = flexmock(DogPlus.new)
|
369
|
-
assert_equal :dog_new, dog.new_instances
|
370
|
-
assert_equal :dog_by_default, dog.by_default
|
371
|
-
end
|
372
|
-
|
373
|
-
def test_should_receive_does_override_should_receive_preexisting_def
|
374
|
-
dog = flexmock(DogPlus.new)
|
375
|
-
assert_kind_of FlexMock::CompositeExpectation, dog.should_receive(:x)
|
376
|
-
end
|
377
|
-
|
378
|
-
class Liar
|
379
|
-
def respond_to?(method_name)
|
380
|
-
sym = method_name.to_sym
|
381
|
-
if sym == :not_defined
|
382
|
-
true
|
383
|
-
else
|
384
|
-
super(method_name)
|
385
|
-
end
|
386
|
-
end
|
387
|
-
end
|
388
|
-
|
389
|
-
def test_liar_actually_lies
|
390
|
-
liar = Liar.new
|
391
|
-
assert liar.respond_to?(:not_defined)
|
392
|
-
assert_raise(NoMethodError) { liar.not_defined }
|
393
|
-
end
|
394
|
-
|
395
|
-
def test_partial_mock_where_respond_to_is_true_yet_method_is_not_there
|
396
|
-
liar = Liar.new
|
397
|
-
flexmock(liar, :not_defined => :xyzzy)
|
398
|
-
assert_equal :xyzzy, liar.not_defined
|
399
|
-
end
|
400
|
-
|
401
|
-
class MetaDog < Dog
|
402
|
-
def method_missing(method, *args, &block)
|
403
|
-
if method.to_s =~ /meow/
|
404
|
-
:meow
|
405
|
-
else
|
406
|
-
super
|
407
|
-
end
|
408
|
-
end
|
409
|
-
def respond_to_missing?(method, *)
|
410
|
-
method =~ /meow/ || super
|
411
|
-
end
|
412
|
-
end
|
413
|
-
|
414
|
-
def test_partial_mock_where_method_created_by_method_missing_and_respond_to_missing
|
415
|
-
dog = MetaDog.new
|
416
|
-
flexmock(dog, :meow => :hiss)
|
417
|
-
assert_equal :hiss, dog.meow
|
418
|
-
end
|
419
|
-
|
420
|
-
def test_partial_mocks_allow_stubbing_defined_methods_when_using_on
|
421
|
-
dog = Dog.new
|
422
|
-
flexmock(dog, :on, Dog)
|
423
|
-
dog.should_receive(:bark).and_return(:grrr)
|
424
|
-
assert_equal :grrr, dog.bark
|
425
|
-
end
|
426
|
-
|
427
|
-
def test_partial_mocks_disallow_stubbing_undefined_methods_when_using_on
|
428
|
-
dog = Dog.new
|
429
|
-
flexmock(dog, :on, Dog)
|
430
|
-
assert_raise(NoMethodError, /meow.*explicitly/) do
|
431
|
-
dog.should_receive(:meow).and_return(:something)
|
432
|
-
end
|
433
|
-
end
|
434
|
-
|
435
|
-
# The following test was suggested by Pat Maddox for the RSpec
|
436
|
-
# mocks. Evidently the (poorly implemented) == method caused issues
|
437
|
-
# with RSpec Mock's internals. I'm just double checking for any
|
438
|
-
# similar issues in FlexMock as well.
|
439
|
-
|
440
|
-
class ValueObject
|
441
|
-
attr_reader :val
|
442
|
-
|
443
|
-
def initialize(val)
|
444
|
-
@val = val
|
445
|
-
end
|
446
|
-
|
447
|
-
def ==(other)
|
448
|
-
@val == other.val
|
449
|
-
end
|
450
|
-
end
|
451
|
-
|
452
|
-
def test_partial_mocks_in_the_presense_of_equal_definition
|
453
|
-
flexmock("existing obj", :foo => :foo)
|
454
|
-
obj = ValueObject.new(:bar)
|
455
|
-
flexmock(obj, :some_method => :some_method)
|
456
|
-
end
|
457
|
-
|
458
|
-
end
|