flexmock 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/Gemfile.lock +20 -0
- data/README.rdoc +3 -2
- data/Rakefile +1 -0
- data/TAGS +393 -297
- data/doc/releases/flexmock-1.0.0.rdoc +6 -6
- data/lib/flexmock/base.rb +4 -0
- data/lib/flexmock/class_extensions.rb +17 -0
- data/lib/flexmock/core.rb +4 -3
- data/lib/flexmock/object_extensions.rb +5 -0
- data/lib/flexmock/partial_mock.rb +2 -4
- data/lib/flexmock/symbol_extensions.rb +17 -0
- data/lib/flexmock/version.rb +1 -1
- data/test/assert_spy_called_test.rb +6 -6
- data/test/object_extensions_test.rb +25 -0
- data/test/partial_mock_test.rb +2 -2
- data/test/should_receive_test.rb +13 -0
- data/test/spys_test.rb +7 -7
- data/test/symbol_extensions_test.rb +8 -0
- data/test/test_class_extensions.rb +34 -0
- metadata +34 -31
@@ -33,12 +33,12 @@ a few bug fixes.
|
|
33
33
|
|
34
34
|
== What is FlexMock?
|
35
35
|
|
36
|
-
FlexMock is a flexible framework for creating mock object for testing.
|
37
|
-
running unit tests, it is often desirable to use isolate the
|
38
|
-
tested from the "real world" by having them interact
|
39
|
-
objects. Sometimes these test objects simply
|
40
|
-
times they verify that certain
|
41
|
-
in a particular order.
|
36
|
+
FlexMock is a flexible framework for creating mock object for testing.
|
37
|
+
When running unit tests, it is often desirable to use isolate the
|
38
|
+
objects being tested from the "real world" by having them interact
|
39
|
+
with simplified test objects. Sometimes these test objects simply
|
40
|
+
return values when called, other times they verify that certain
|
41
|
+
methods were called with particular arguments in a particular order.
|
42
42
|
|
43
43
|
FlexMock makes creating these test objects easy.
|
44
44
|
|
data/lib/flexmock/base.rb
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Detecting whether a class has a definition for a method or not
|
2
|
+
# changes between Ruby 1.8 and Ruby 1.9. We introduce the
|
3
|
+
# "flexmock_defined?" method on class objects to have a portable way
|
4
|
+
# to determine that.
|
5
|
+
#
|
6
|
+
# NOTE: responds_to? isn't appropriate. We don't care if the object
|
7
|
+
# responds to the method or not. We want to know if the class
|
8
|
+
# has a definition for the method. A subtle difference.
|
9
|
+
#
|
10
|
+
class Class
|
11
|
+
|
12
|
+
# Does a class directly define a method named "method_name"?
|
13
|
+
def flexmock_defined?(method_name)
|
14
|
+
instance_methods.include?(method_name.flexmock_as_name)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
data/lib/flexmock/core.rb
CHANGED
@@ -14,6 +14,7 @@ require 'flexmock/composite'
|
|
14
14
|
require 'flexmock/ordering'
|
15
15
|
require 'flexmock/argument_matching'
|
16
16
|
require 'flexmock/explicit_needed'
|
17
|
+
require 'flexmock/class_extensions'
|
17
18
|
|
18
19
|
######################################################################
|
19
20
|
# FlexMock is a flexible mock object framework for supporting testing.
|
@@ -105,7 +106,7 @@ class FlexMock
|
|
105
106
|
if handler = @expectations[sym]
|
106
107
|
args << block if block_given?
|
107
108
|
handler.call(*args)
|
108
|
-
elsif @base_class && @base_class.
|
109
|
+
elsif @base_class && @base_class.flexmock_defined?(sym)
|
109
110
|
FlexMock.undefined
|
110
111
|
elsif @ignore_missing
|
111
112
|
FlexMock.undefined
|
@@ -136,7 +137,7 @@ class FlexMock
|
|
136
137
|
|
137
138
|
def flexmock_based_on(base_class)
|
138
139
|
@base_class = base_class
|
139
|
-
should_receive(class
|
140
|
+
should_receive(:class => base_class)
|
140
141
|
end
|
141
142
|
|
142
143
|
# True if the mock received the given method and arguments.
|
@@ -202,7 +203,7 @@ class FlexMock
|
|
202
203
|
@expectations[sym] << result
|
203
204
|
override_existing_method(sym) if flexmock_respond_to?(sym)
|
204
205
|
result = ExplicitNeeded.new(result, sym, @base_class) if
|
205
|
-
@base_class && ! @base_class.
|
206
|
+
@base_class && ! @base_class.flexmock_defined?(sym)
|
206
207
|
result
|
207
208
|
end
|
208
209
|
end
|
@@ -209,10 +209,8 @@ class FlexMock
|
|
209
209
|
class << @obj; self; end
|
210
210
|
end
|
211
211
|
|
212
|
-
# Is the given method name a singleton method in the object we are
|
213
|
-
# mocking?
|
214
212
|
def singleton?(method_name)
|
215
|
-
@obj.
|
213
|
+
@obj.flexmock_singleton_defined?(method_name)
|
216
214
|
end
|
217
215
|
|
218
216
|
# Hide the existing method definition with a singleton defintion
|
@@ -264,7 +262,7 @@ class FlexMock
|
|
264
262
|
sclass.class_eval do
|
265
263
|
begin
|
266
264
|
alias_method(new_alias, method_name)
|
267
|
-
rescue NameError
|
265
|
+
rescue NameError
|
268
266
|
nil
|
269
267
|
end
|
270
268
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class Symbol
|
2
|
+
|
3
|
+
case instance_methods.first
|
4
|
+
when Symbol
|
5
|
+
def flexmock_as_name
|
6
|
+
self
|
7
|
+
end
|
8
|
+
|
9
|
+
when String
|
10
|
+
def flexmock_as_name
|
11
|
+
to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
else
|
15
|
+
fail "Unexpected class for method list #{instance_methods.first.class}"
|
16
|
+
end
|
17
|
+
end
|
data/lib/flexmock/version.rb
CHANGED
@@ -34,7 +34,7 @@ class AssertSpyCalledTest < Test::Unit::TestCase
|
|
34
34
|
|
35
35
|
def test_assert_rejects_incorrect_args
|
36
36
|
spy.foo(1,2)
|
37
|
-
|
37
|
+
assert_fails(/^expected foo\(1, 3\) to be received by <FlexMock:AssertSpyCalledTest::FooBar Mock>/i) do
|
38
38
|
assert_spy_called spy, :foo, 1, 3
|
39
39
|
end
|
40
40
|
end
|
@@ -43,14 +43,14 @@ class AssertSpyCalledTest < Test::Unit::TestCase
|
|
43
43
|
spy.foo
|
44
44
|
spy.foo
|
45
45
|
spy.foo
|
46
|
-
assert_spy_called spy, {times
|
46
|
+
assert_spy_called spy, {:times => 3}, :foo
|
47
47
|
end
|
48
48
|
|
49
49
|
def test_assert_rejects_incorrect_type
|
50
50
|
spy.foo
|
51
51
|
spy.foo
|
52
52
|
assert_fails(/^expected foo\(\) to be received by <FlexMock:AssertSpyCalledTest::FooBar Mock> 3 times/i) do
|
53
|
-
assert_spy_called spy, {times
|
53
|
+
assert_spy_called spy, {:times => 3}, :foo
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
@@ -65,14 +65,14 @@ class AssertSpyCalledTest < Test::Unit::TestCase
|
|
65
65
|
spy.foo
|
66
66
|
spy.foo(1)
|
67
67
|
spy.foo("HI")
|
68
|
-
spy.foo("Hello", "World", 10, options
|
69
|
-
assert_spy_called spy, {times
|
68
|
+
spy.foo("Hello", "World", 10, :options => true)
|
69
|
+
assert_spy_called spy, {:times => 4}, :foo, :_
|
70
70
|
end
|
71
71
|
|
72
72
|
def test_assert_rejects_bad_count_on_any_args
|
73
73
|
spy.foo
|
74
74
|
assert_fails(/^expected foo\(\.\.\.\) to be received by <FlexMock:AssertSpyCalledTest::FooBar Mock> twice/i) do
|
75
|
-
assert_spy_called spy, {times
|
75
|
+
assert_spy_called spy, {:times => 2}, :foo, :_
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
@@ -0,0 +1,25 @@
|
|
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
CHANGED
@@ -38,7 +38,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
38
38
|
def test_attempting_to_partially_mock_existing_mock_is_noop
|
39
39
|
m = flexmock("A")
|
40
40
|
flexmock(m)
|
41
|
-
assert ! m.instance_variables.include?(:@flexmock_proxy)
|
41
|
+
assert ! m.instance_variables.include?(:@flexmock_proxy.flexmock_as_name)
|
42
42
|
end
|
43
43
|
|
44
44
|
def test_stub_command_add_behavior_to_arbitrary_objects
|
@@ -287,7 +287,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
287
287
|
end
|
288
288
|
end
|
289
289
|
|
290
|
-
def
|
290
|
+
def xtest_object_methods_method_is_not_used_in_singleton_checks
|
291
291
|
obj = NoMethods.new
|
292
292
|
def obj.mock() :original end
|
293
293
|
assert_nothing_raised { flexmock(obj) }
|
data/test/should_receive_test.rb
CHANGED
@@ -149,6 +149,19 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
149
149
|
end
|
150
150
|
end
|
151
151
|
|
152
|
+
def test_multiple_yields_and_multiple_returns_are_synced
|
153
|
+
FlexMock.use do |m|
|
154
|
+
m.should_receive(:msg).and_yield(:one).and_return(1).and_yield(:two).and_return(2)
|
155
|
+
yielded_values = []
|
156
|
+
returned_values = []
|
157
|
+
returned_values << m.msg { |a| yielded_values << a }
|
158
|
+
returned_values << m.msg { |a| yielded_values << a }
|
159
|
+
returned_values << m.msg { |a| yielded_values << a }
|
160
|
+
assert_equal [:one, :two, :two], yielded_values
|
161
|
+
assert_equal [1, 2, 2], returned_values
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
152
165
|
def test_failure_if_no_block_given
|
153
166
|
FlexMock.use do |m|
|
154
167
|
m.should_receive(:hi).and_yield(:one, :two).once
|
data/test/spys_test.rb
CHANGED
@@ -56,7 +56,7 @@ class TestSpys < Test::Unit::TestCase
|
|
56
56
|
def test_spy_detects_multiple_calls_with_different_arguments
|
57
57
|
@spy.foo(1)
|
58
58
|
@spy.foo(1)
|
59
|
-
assert_spy_called @spy, {times
|
59
|
+
assert_spy_called @spy, {:times => 2}, :foo, 1
|
60
60
|
end
|
61
61
|
|
62
62
|
def test_spy_rejects_if_times_options_not_matching
|
@@ -72,12 +72,12 @@ class TestSpys < Test::Unit::TestCase
|
|
72
72
|
|
73
73
|
def test_spy_rejects_a_block
|
74
74
|
@spy.foo { }
|
75
|
-
assert_spy_not_called @spy, {with_block
|
75
|
+
assert_spy_not_called @spy, {:with_block => false}, :foo
|
76
76
|
end
|
77
77
|
|
78
78
|
def test_spy_detects_a_missing_block
|
79
79
|
@spy.foo
|
80
|
-
assert_spy_called @spy, {with_block
|
80
|
+
assert_spy_called @spy, {:with_block => false}, :foo
|
81
81
|
end
|
82
82
|
|
83
83
|
def test_spy_rejects_a_missing_block
|
@@ -119,7 +119,7 @@ class TestSpys < Test::Unit::TestCase
|
|
119
119
|
|
120
120
|
def test_cant_put_expectations_on_non_base_class_methodsx
|
121
121
|
ex = assert_raise(NoMethodError) do
|
122
|
-
|
122
|
+
@spy.should_receive(:baz).and_return(:bar)
|
123
123
|
end
|
124
124
|
assert_match(/cannot stub.*defined.*base.*class/i, ex.message)
|
125
125
|
assert_match(/method: +baz/i, ex.message)
|
@@ -127,13 +127,13 @@ class TestSpys < Test::Unit::TestCase
|
|
127
127
|
end
|
128
128
|
|
129
129
|
def test_cant_put_expectations_on_non_base_class_methods_unless_explicit
|
130
|
-
|
130
|
+
@spy.should_receive(:baz).explicitly.and_return(:bar)
|
131
131
|
@spy.baz
|
132
132
|
assert_spy_called @spy, :baz
|
133
133
|
end
|
134
134
|
|
135
135
|
def test_ok_to_use_explicit_even_when_its_not_needed
|
136
|
-
|
136
|
+
@spy.should_receive(:foo).explicitly.and_return(:bar)
|
137
137
|
@spy.foo
|
138
138
|
assert_spy_called @spy, :foo
|
139
139
|
end
|
@@ -150,7 +150,7 @@ class TestSpys < Test::Unit::TestCase
|
|
150
150
|
def test_can_spy_on_class_defined_methods
|
151
151
|
flexmock(FooBar).should_receive(:new).and_return(:dummy)
|
152
152
|
FooBar.new
|
153
|
-
assert_spy_called FooBar, :new
|
153
|
+
assert_spy_called FooBar, :new
|
154
154
|
end
|
155
155
|
|
156
156
|
def test_can_spy_on_regular_mocks
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test/test_setup'
|
2
|
+
|
3
|
+
class ClassExtensionsTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
class Dog
|
6
|
+
def wag
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(sym, *args, &block)
|
10
|
+
if sym == :bark
|
11
|
+
:woof
|
12
|
+
else
|
13
|
+
super
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def responds_to?(sym)
|
18
|
+
sym == :bark || super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_class_directly_defines_method
|
23
|
+
assert Dog.flexmock_defined?(:wag)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_class_indirectly_defines_method
|
27
|
+
assert ! Dog.flexmock_defined?(:bark)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_class_does_not_define_method
|
31
|
+
assert ! Dog.flexmock_defined?(:jump)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
metadata
CHANGED
@@ -1,29 +1,25 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: flexmock
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
4
5
|
prerelease:
|
5
|
-
version: 1.0.0
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Jim Weirich
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
date: 2012-08-23 00:00:00 Z
|
12
|
+
date: 2012-09-06 00:00:00.000000000 Z
|
14
13
|
dependencies: []
|
15
|
-
|
16
|
-
|
14
|
+
description: ! "\n FlexMock is a extremely simple mock object class compatible\n
|
15
|
+
\ with the Test::Unit framework. Although the FlexMock's\n interface is
|
16
|
+
simple, it is very flexible.\n "
|
17
17
|
email: jim.weirich@gmail.com
|
18
18
|
executables: []
|
19
|
-
|
20
19
|
extensions: []
|
21
|
-
|
22
|
-
extra_rdoc_files:
|
20
|
+
extra_rdoc_files:
|
23
21
|
- README.rdoc
|
24
22
|
- CHANGES
|
25
|
-
- doc/examples/rspec_examples_spec.rdoc
|
26
|
-
- doc/examples/test_unit_examples_test.rdoc
|
27
23
|
- doc/GoogleExample.rdoc
|
28
24
|
- doc/releases/flexmock-0.4.0.rdoc
|
29
25
|
- doc/releases/flexmock-0.4.1.rdoc
|
@@ -45,8 +41,12 @@ extra_rdoc_files:
|
|
45
41
|
- doc/releases/flexmock-0.8.5.rdoc
|
46
42
|
- doc/releases/flexmock-0.9.0.rdoc
|
47
43
|
- doc/releases/flexmock-1.0.0.rdoc
|
48
|
-
|
44
|
+
- doc/examples/rspec_examples_spec.rdoc
|
45
|
+
- doc/examples/test_unit_examples_test.rdoc
|
46
|
+
files:
|
49
47
|
- CHANGES
|
48
|
+
- Gemfile
|
49
|
+
- Gemfile.lock
|
50
50
|
- Rakefile
|
51
51
|
- README.rdoc
|
52
52
|
- TAGS
|
@@ -54,6 +54,7 @@ files:
|
|
54
54
|
- lib/flexmock/argument_matching.rb
|
55
55
|
- lib/flexmock/argument_types.rb
|
56
56
|
- lib/flexmock/base.rb
|
57
|
+
- lib/flexmock/class_extensions.rb
|
57
58
|
- lib/flexmock/composite.rb
|
58
59
|
- lib/flexmock/core.rb
|
59
60
|
- lib/flexmock/core_class_methods.rb
|
@@ -65,6 +66,7 @@ files:
|
|
65
66
|
- lib/flexmock/explicit_needed.rb
|
66
67
|
- lib/flexmock/mock_container.rb
|
67
68
|
- lib/flexmock/noop.rb
|
69
|
+
- lib/flexmock/object_extensions.rb
|
68
70
|
- lib/flexmock/ordering.rb
|
69
71
|
- lib/flexmock/partial_mock.rb
|
70
72
|
- lib/flexmock/rails/view_mocking.rb
|
@@ -73,6 +75,7 @@ files:
|
|
73
75
|
- lib/flexmock/rspec.rb
|
74
76
|
- lib/flexmock/rspec_spy_matcher.rb
|
75
77
|
- lib/flexmock/spy_describers.rb
|
78
|
+
- lib/flexmock/symbol_extensions.rb
|
76
79
|
- lib/flexmock/test_unit.rb
|
77
80
|
- lib/flexmock/test_unit_assert_spy_called.rb
|
78
81
|
- lib/flexmock/test_unit_integration.rb
|
@@ -92,6 +95,7 @@ files:
|
|
92
95
|
- test/flexmodel_test.rb
|
93
96
|
- test/naming_test.rb
|
94
97
|
- test/new_instances_test.rb
|
98
|
+
- test/object_extensions_test.rb
|
95
99
|
- test/partial_mock_test.rb
|
96
100
|
- test/rails_view_stub_test.rb
|
97
101
|
- test/record_mode_test.rb
|
@@ -102,14 +106,14 @@ files:
|
|
102
106
|
- test/should_ignore_missing_test.rb
|
103
107
|
- test/should_receive_test.rb
|
104
108
|
- test/spys_test.rb
|
109
|
+
- test/symbol_extensions_test.rb
|
110
|
+
- test/test_class_extensions.rb
|
105
111
|
- test/test_setup.rb
|
106
112
|
- test/test_unit_integration/auto_test_unit_test.rb
|
107
113
|
- test/tu_integration_test.rb
|
108
114
|
- test/undefined_test.rb
|
109
115
|
- flexmock.blurb
|
110
116
|
- install.rb
|
111
|
-
- doc/examples/rspec_examples_spec.rdoc
|
112
|
-
- doc/examples/test_unit_examples_test.rdoc
|
113
117
|
- doc/GoogleExample.rdoc
|
114
118
|
- doc/releases/flexmock-0.4.0.rdoc
|
115
119
|
- doc/releases/flexmock-0.4.1.rdoc
|
@@ -131,36 +135,35 @@ files:
|
|
131
135
|
- doc/releases/flexmock-0.8.5.rdoc
|
132
136
|
- doc/releases/flexmock-0.9.0.rdoc
|
133
137
|
- doc/releases/flexmock-1.0.0.rdoc
|
138
|
+
- doc/examples/rspec_examples_spec.rdoc
|
139
|
+
- doc/examples/test_unit_examples_test.rdoc
|
134
140
|
homepage: https://github.com/jimweirich/flexmock
|
135
141
|
licenses: []
|
136
|
-
|
137
142
|
post_install_message:
|
138
|
-
rdoc_options:
|
143
|
+
rdoc_options:
|
139
144
|
- --title
|
140
145
|
- FlexMock
|
141
146
|
- --main
|
142
147
|
- README.rdoc
|
143
148
|
- --line-numbers
|
144
|
-
require_paths:
|
149
|
+
require_paths:
|
145
150
|
- lib
|
146
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
151
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
152
|
none: false
|
148
|
-
requirements:
|
149
|
-
- -
|
150
|
-
- !ruby/object:Gem::Version
|
151
|
-
version:
|
152
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - ! '>='
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
153
158
|
none: false
|
154
|
-
requirements:
|
155
|
-
- -
|
156
|
-
- !ruby/object:Gem::Version
|
157
|
-
version:
|
159
|
+
requirements:
|
160
|
+
- - ! '>='
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
158
163
|
requirements: []
|
159
|
-
|
160
164
|
rubyforge_project:
|
161
165
|
rubygems_version: 1.8.15
|
162
166
|
signing_key:
|
163
167
|
specification_version: 3
|
164
168
|
summary: Simple and Flexible Mock Objects for Testing
|
165
169
|
test_files: []
|
166
|
-
|