flexmock 1.0.0.beta.2 → 1.0.0.beta.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +7 -7
- data/lib/flexmock/core.rb +13 -3
- data/lib/flexmock/expectation.rb +1 -1
- data/lib/flexmock/explicit_needed.rb +2 -2
- data/lib/flexmock/mock_container.rb +1 -1
- data/lib/flexmock/partial_mock.rb +8 -3
- data/lib/flexmock/rspec_spy_matcher.rb +1 -1
- data/lib/flexmock/spy_describers.rb +5 -1
- data/lib/flexmock/test_unit_assert_spy_called.rb +1 -1
- data/lib/flexmock/version.rb +1 -1
- data/test/base_class_test.rb +52 -0
- data/test/spys_test.rb +4 -5
- data/test/test_setup.rb +0 -1
- metadata +2 -1
data/README.rdoc
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
FlexMock is a simple, but flexible, mock object library for Ruby unit
|
4
4
|
testing.
|
5
5
|
|
6
|
-
Version :: 1.0.0.beta.
|
6
|
+
Version :: 1.0.0.beta.3
|
7
7
|
|
8
8
|
= Links
|
9
9
|
|
@@ -141,7 +141,7 @@ FlexMock::MockContainer#flexmock for more details.
|
|
141
141
|
teh base class. This helps prevent tests from becoming stale with
|
142
142
|
incorrectly mocked objects when the method names change.
|
143
143
|
|
144
|
-
Use the <code>
|
144
|
+
Use the <code>explicitly</code> modifier to
|
145
145
|
<code>should_receive</code> to override base class restrictions.
|
146
146
|
|
147
147
|
* <b>partial_mock = flexmock(real_object)</b>
|
@@ -271,13 +271,13 @@ a mock object. See theFlexMock::Expectation for more details.
|
|
271
271
|
Define a number of expected messages that have the same constrants, but
|
272
272
|
return different values.
|
273
273
|
|
274
|
-
* <b>should_receive(...).
|
274
|
+
* <b>should_receive(...).explicitly</b>
|
275
275
|
|
276
|
-
If a mock has a base class, use the <code>
|
276
|
+
If a mock has a base class, use the <code>explicitly</code> modifier
|
277
277
|
to override the restriction on method names imposed by the base
|
278
|
-
class. The
|
279
|
-
<code>should_receive</code> call and before any other
|
280
|
-
declarators.
|
278
|
+
class. The <code>explicitly</code> modifier must come immediately
|
279
|
+
after the <code>should_receive</code> call and before any other
|
280
|
+
expectation declarators.
|
281
281
|
|
282
282
|
If a mock does not have a base class, this method has no effect.
|
283
283
|
|
data/lib/flexmock/core.rb
CHANGED
@@ -134,13 +134,15 @@ class FlexMock
|
|
134
134
|
@expectations[method_name]
|
135
135
|
end
|
136
136
|
|
137
|
-
def
|
137
|
+
def flexmock_based_on(base_class)
|
138
138
|
@base_class = base_class
|
139
|
+
should_receive(class: base_class)
|
139
140
|
end
|
140
141
|
|
141
|
-
|
142
|
+
# True if the mock received the given method and arguments.
|
143
|
+
def flexmock_received?(sym, args, options={})
|
142
144
|
count = 0
|
143
|
-
@calls.each { |call_sym, call_args
|
145
|
+
@calls.each { |call_sym, call_args|
|
144
146
|
count += 1 if (call_sym == sym) && ArgumentMatching.all_match?(args, call_args)
|
145
147
|
}
|
146
148
|
if options[:times]
|
@@ -151,6 +153,14 @@ class FlexMock
|
|
151
153
|
result
|
152
154
|
end
|
153
155
|
|
156
|
+
# Return the list of calls made on this mock. Used in formatting
|
157
|
+
# error messages.
|
158
|
+
def flexmock_calls
|
159
|
+
@calls
|
160
|
+
end
|
161
|
+
|
162
|
+
# Invocke the original non-mocked functionality for the given
|
163
|
+
# symbol.
|
154
164
|
def flexmock_invoke_original(sym, args)
|
155
165
|
return FlexMock.undefined
|
156
166
|
end
|
data/lib/flexmock/expectation.rb
CHANGED
@@ -16,7 +16,7 @@ class FlexMock
|
|
16
16
|
@base_class = base_class
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
19
|
+
def explicitly
|
20
20
|
@explicit = true
|
21
21
|
self
|
22
22
|
end
|
@@ -32,7 +32,7 @@ class FlexMock
|
|
32
32
|
fail NoMethodError, "Cannot stub methods not defined by the base class\n" +
|
33
33
|
" Method: #{@method_name}\n" +
|
34
34
|
" Base Class: #{@base_class}\n" +
|
35
|
-
" (Use
|
35
|
+
" (Use 'explicitly' to override)"
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
@@ -158,7 +158,7 @@ class FlexMock
|
|
158
158
|
yield(mock) if block_given?
|
159
159
|
flexmock_remember(mock)
|
160
160
|
ContainerHelper.add_model_methods(mock, model_class, id) if model_class
|
161
|
-
mock.
|
161
|
+
mock.flexmock_based_on(base_class) if base_class
|
162
162
|
result
|
163
163
|
end
|
164
164
|
alias flexstub flexmock
|
@@ -34,7 +34,7 @@ class FlexMock
|
|
34
34
|
MOCK_METHODS = [
|
35
35
|
:should_receive, :new_instances,
|
36
36
|
:flexmock_get, :flexmock_teardown, :flexmock_verify,
|
37
|
-
:
|
37
|
+
:flexmock_received?, :flexmock_calls,
|
38
38
|
]
|
39
39
|
|
40
40
|
# Initialize a PartialMockProxy object.
|
@@ -177,8 +177,13 @@ class FlexMock
|
|
177
177
|
end
|
178
178
|
|
179
179
|
# Forward to the mock
|
180
|
-
def
|
181
|
-
@mock.
|
180
|
+
def flexmock_received?(*args)
|
181
|
+
@mock.flexmock_received?(*args)
|
182
|
+
end
|
183
|
+
|
184
|
+
# Forward to the mock
|
185
|
+
def flexmock_calls
|
186
|
+
@mock.flexmock_calls
|
182
187
|
end
|
183
188
|
|
184
189
|
# Set the proxy's mock container. This set value is ignored
|
@@ -19,7 +19,7 @@ class FlexMock
|
|
19
19
|
@options = {}
|
20
20
|
@options[:times] = @times if @times
|
21
21
|
@options[:with_block] = @needs_block unless @needs_block.nil?
|
22
|
-
@spy.
|
22
|
+
@spy.flexmock_received?(@method_name, @args, @options)
|
23
23
|
end
|
24
24
|
|
25
25
|
def failure_message_for_should
|
@@ -18,7 +18,11 @@ class FlexMock
|
|
18
18
|
result << spy.inspect
|
19
19
|
result << times_description(options[:times])
|
20
20
|
result << block_description(options[:with_block])
|
21
|
-
result << "
|
21
|
+
result << ".\n"
|
22
|
+
result << "The following messages have been received:\n"
|
23
|
+
spy.flexmock_calls.each do |sym, args|
|
24
|
+
result << " " << call_description(sym, args) << "\n"
|
25
|
+
end
|
22
26
|
result
|
23
27
|
end
|
24
28
|
|
@@ -21,7 +21,7 @@ class FlexMock
|
|
21
21
|
method_name = args.shift
|
22
22
|
end
|
23
23
|
args = nil if args == [:_]
|
24
|
-
bool = spy.
|
24
|
+
bool = spy.flexmock_received?(method_name, args, options)
|
25
25
|
if negative
|
26
26
|
bool = !bool
|
27
27
|
message = describe_spy_negative_expectation(spy, method_name, args, options)
|
data/lib/flexmock/version.rb
CHANGED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'test/test_setup'
|
2
|
+
|
3
|
+
class BaseClassTest < Test::Unit::TestCase
|
4
|
+
include FlexMock::TestCase
|
5
|
+
|
6
|
+
class FooBar
|
7
|
+
def foo
|
8
|
+
end
|
9
|
+
|
10
|
+
def method_missing(sym, *args, &block)
|
11
|
+
return :poof if sym == :barq
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def respond_to?(method)
|
16
|
+
method == :barq || super
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def mock
|
21
|
+
@mock ||= flexmock(:on, FooBar)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_auto_mocks_class
|
25
|
+
assert_equal FooBar, mock.class
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_can_stub_existing_methods
|
29
|
+
mock.should_receive(:foo => :bar)
|
30
|
+
assert_equal :bar, mock.foo
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_can_not_stub_non_class_defined_methods
|
34
|
+
ex = assert_raises(NoMethodError) do
|
35
|
+
mock.should_receive(:baz => :bar)
|
36
|
+
end
|
37
|
+
assert_match(/can *not stub methods.*base.*class/i, ex.message)
|
38
|
+
assert_match(/class:.+FooBar/i, ex.message)
|
39
|
+
assert_match(/method:.+baz/i, ex.message)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_can_explicitly_stub_non_class_defined_methods
|
43
|
+
mock.should_receive(:baz).explicitly.and_return(:bar)
|
44
|
+
assert_equal :bar, mock.baz
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_can_explicitly_stub_meta_programmed_methods
|
48
|
+
mock.should_receive(:barq).explicitly.and_return(:bar)
|
49
|
+
assert_equal :bar, mock.barq
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
data/test/spys_test.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require 'test/test_setup'
|
4
|
-
require 'flexmock/spy_describers'
|
5
4
|
|
6
5
|
class TestSpys < Test::Unit::TestCase
|
7
6
|
include FlexMock::TestCase
|
@@ -94,7 +93,7 @@ class TestSpys < Test::Unit::TestCase
|
|
94
93
|
@spy.should_receive(:foo).and_return(:hi)
|
95
94
|
result = @spy.foo
|
96
95
|
assert_equal result, :hi
|
97
|
-
|
96
|
+
assert_spy_called @spy, :foo
|
98
97
|
end
|
99
98
|
|
100
99
|
def test_calling_non_spy_base_methods_is_an_error
|
@@ -113,13 +112,13 @@ class TestSpys < Test::Unit::TestCase
|
|
113
112
|
end
|
114
113
|
|
115
114
|
def test_cant_put_expectations_on_non_base_class_methods_unless_explicit
|
116
|
-
exp = @spy.should_receive(:baz).
|
115
|
+
exp = @spy.should_receive(:baz).explicitly.and_return(:bar)
|
117
116
|
@spy.baz
|
118
117
|
assert_spy_called @spy, :baz
|
119
118
|
end
|
120
119
|
|
121
120
|
def test_ok_to_use_explicit_even_when_its_not_needed
|
122
|
-
exp = @spy.should_receive(:foo).
|
121
|
+
exp = @spy.should_receive(:foo).explicitly.and_return(:bar)
|
123
122
|
@spy.foo
|
124
123
|
assert_spy_called @spy, :foo
|
125
124
|
end
|
@@ -133,7 +132,7 @@ class TestSpys < Test::Unit::TestCase
|
|
133
132
|
assert_spy_called @foo, :foo
|
134
133
|
end
|
135
134
|
|
136
|
-
def
|
135
|
+
def test_can_spy_on_class_defined_methods
|
137
136
|
flexmock(FooBar).should_receive(:new).and_return(:dummy)
|
138
137
|
FooBar.new
|
139
138
|
assert_spy_called FooBar, :new,
|
data/test/test_setup.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: flexmock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: 6
|
5
|
-
version: 1.0.0.beta.
|
5
|
+
version: 1.0.0.beta.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jim Weirich
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- lib/flexmock.rb
|
81
81
|
- test/aliasing_test.rb
|
82
82
|
- test/assert_spy_called_test.rb
|
83
|
+
- test/base_class_test.rb
|
83
84
|
- test/container_methods_test.rb
|
84
85
|
- test/default_framework_adapter_test.rb
|
85
86
|
- test/demeter_mocking_test.rb
|