flexmock 0.4.0 → 0.4.1
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.
- data/CHANGELOG +6 -1
- data/README +1 -1
- data/Rakefile +4 -2
- data/doc/releases/flexmock-0.4.1.rdoc +87 -0
- data/lib/flexmock.rb +20 -0
- data/test/test_should_receive.rb +31 -0
- data/test/test_stubbing.rb +9 -0
- metadata +4 -2
data/CHANGELOG
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
= Changes for FlexMock
|
2
2
|
|
3
|
-
==
|
3
|
+
== Version 0.4.1
|
4
|
+
|
5
|
+
* Removed include of Test::Unit::Assertions from Expectations.
|
6
|
+
* Fixed mocking of kernel methods.
|
7
|
+
|
8
|
+
== Version 0.4.0
|
4
9
|
|
5
10
|
* Added stubbing for mocking methods of existing objects.
|
6
11
|
* Added multiple return values for the +and_returns+ method.
|
data/README
CHANGED
data/Rakefile
CHANGED
@@ -9,7 +9,7 @@ require 'rake/testtask'
|
|
9
9
|
CLEAN.include('*.tmp')
|
10
10
|
CLOBBER.include("html", 'pkg')
|
11
11
|
|
12
|
-
PKG_VERSION = '0.4.
|
12
|
+
PKG_VERSION = '0.4.1'
|
13
13
|
|
14
14
|
PKG_FILES = FileList[
|
15
15
|
'[A-Z]*',
|
@@ -147,6 +147,8 @@ task :tag do
|
|
147
147
|
sh %{svn copy #{SVNHOME}/trunk #{SVNHOME}/tags/rel-#{PKG_VERSION} -m 'Release #{PKG_VERSION}'}
|
148
148
|
end
|
149
149
|
|
150
|
+
RUBY_FILES = FileList['**/*.rb']
|
151
|
+
RUBY_FILES.exclude(/^pkg/)
|
150
152
|
task :dbg do
|
151
|
-
|
153
|
+
RUBY_FILES.egrep(/DBG/)
|
152
154
|
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
= FlexMock 0.4.1 Released
|
2
|
+
|
3
|
+
FlexMock is a flexible mocking library for use in Ruby's Test::Unit
|
4
|
+
test framework. Version 0.4.0 enhances FlexMock with the ability to
|
5
|
+
stub and mock methods in existing objects.
|
6
|
+
|
7
|
+
== New in 0.4.1
|
8
|
+
|
9
|
+
Coming fast on the heels of 0.4.0, version 0.4.1 fixes a problem with
|
10
|
+
mocking methods that have the same name as methods defined in Kernel.
|
11
|
+
|
12
|
+
== What is FlexMock?
|
13
|
+
|
14
|
+
FlexMock is a flexible Ruby mocking library that works with Ruby's
|
15
|
+
Test::Unit framework to create easy to use mocks.
|
16
|
+
|
17
|
+
=== Features
|
18
|
+
|
19
|
+
* Easy integration with Test::Unit. Mocks created with the flexmock
|
20
|
+
method are automatically verified at the end of the test.
|
21
|
+
|
22
|
+
* A fluent interface that allows mock behavior to be specified very
|
23
|
+
easily.
|
24
|
+
|
25
|
+
* A "record mode" where an existing implementation can record its
|
26
|
+
interaction with a mock for later validation against a new
|
27
|
+
implementation.
|
28
|
+
|
29
|
+
* Easy mocking of individual methods in existing, non-mock objects.
|
30
|
+
|
31
|
+
=== Example
|
32
|
+
|
33
|
+
Suppose you had a Dog object that wagged a tail when it was happy.
|
34
|
+
Something like this:
|
35
|
+
|
36
|
+
class Dog
|
37
|
+
def initialize(a_tail)
|
38
|
+
@tail = a_tail
|
39
|
+
end
|
40
|
+
def happy
|
41
|
+
@tail.wag
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
To test the +Dog+ class without a real +Tail+ object (perhaps because
|
46
|
+
real +Tail+ objects activate servos in some robotic equipment), you
|
47
|
+
can do something like this:
|
48
|
+
|
49
|
+
require 'test/unit'
|
50
|
+
require 'flexmock'
|
51
|
+
|
52
|
+
class TestDog < Test::Unit::TestCase
|
53
|
+
include FlexMock::TestCase
|
54
|
+
|
55
|
+
def test_dog_wags_tail_when_happy
|
56
|
+
tail = flexmock("tail")
|
57
|
+
tail.should_receive(:wag).once
|
58
|
+
dog = Dog.new(tail)
|
59
|
+
dog.happy
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
FlexMock will automatically verify that the mocked tail object
|
64
|
+
received the message +wag+ exactly one time. If it doesn't, the test
|
65
|
+
will not pass.
|
66
|
+
|
67
|
+
See the FlexMock documentation at
|
68
|
+
http://onestepback.org/software/flexmock for details on specifying
|
69
|
+
arguments and return values on mocked methods, as well as a simple
|
70
|
+
technique for mocking tail objects when the Dog class creates the tail
|
71
|
+
objects directly.
|
72
|
+
|
73
|
+
== Availability
|
74
|
+
|
75
|
+
FlexMock is distributed with Rails, or you can make sure you have the
|
76
|
+
latest version with a quick RubyGems command:
|
77
|
+
|
78
|
+
gem install flexmock (you may need root/admin privileges)
|
79
|
+
|
80
|
+
Otherwise, you can get it from the more traditional places:
|
81
|
+
|
82
|
+
Download:: http://rubyforge.org/project/showfiles.php?group_id=170
|
83
|
+
|
84
|
+
You will find documentation at:
|
85
|
+
http://onestepback.org/software/flexmock/
|
86
|
+
|
87
|
+
-- Jim Weirich
|
data/lib/flexmock.rb
CHANGED
@@ -115,6 +115,9 @@ class FlexMock
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
+
# Save the original definition of respond_to? for use a bit later.
|
119
|
+
alias mock_respond_to? respond_to?
|
120
|
+
|
118
121
|
# Override the built-in respond_to? to include the mocked methods.
|
119
122
|
def respond_to?(sym)
|
120
123
|
super || (@expectations[sym] ? true : @ignore_missing)
|
@@ -141,9 +144,26 @@ class FlexMock
|
|
141
144
|
@expectations[sym] ||= ExpectationDirector.new(sym)
|
142
145
|
result = Expectation.new(self, sym)
|
143
146
|
@expectations[sym] << result
|
147
|
+
override_existing_method(sym) if mock_respond_to?(sym)
|
144
148
|
result
|
145
149
|
end
|
146
150
|
|
151
|
+
# Override the existing definition of method +sym+ in the mock.
|
152
|
+
# Most methods depend on the method_missing trick to be invoked.
|
153
|
+
# However, if the method already exists, it will not call
|
154
|
+
# method_missing. This method defines a singleton method on the
|
155
|
+
# mock to explicitly invoke the method_missing logic.
|
156
|
+
def override_existing_method(sym)
|
157
|
+
sclass.class_eval "def #{sym}(*args, &block) method_missing(:#{sym}, *args, &block) end"
|
158
|
+
end
|
159
|
+
private :override_existing_method
|
160
|
+
|
161
|
+
# Return the singleton class of the mock object.
|
162
|
+
def sclass
|
163
|
+
class << self; self; end
|
164
|
+
end
|
165
|
+
private :sclass
|
166
|
+
|
147
167
|
# Declare that the mock object should expect methods by providing a
|
148
168
|
# recorder for the methods and having the user invoke the expected
|
149
169
|
# methods in a block. Further expectations may be applied the
|
data/test/test_should_receive.rb
CHANGED
@@ -12,6 +12,17 @@
|
|
12
12
|
require 'test/unit'
|
13
13
|
require 'flexmock'
|
14
14
|
|
15
|
+
def mock_top_level_function
|
16
|
+
:mtlf
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
module Kernel
|
21
|
+
def mock_kernel_function
|
22
|
+
:mkf
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
15
26
|
class TestFlexMockShoulds < Test::Unit::TestCase
|
16
27
|
|
17
28
|
def test_defaults
|
@@ -566,6 +577,26 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
566
577
|
end
|
567
578
|
end
|
568
579
|
|
580
|
+
def test_global_methods_can_be_mocked
|
581
|
+
m = FlexMock.new("m")
|
582
|
+
m.should_receive(:mock_top_level_function).and_return(:mock)
|
583
|
+
assert_equal :mock, m.mock_top_level_function
|
584
|
+
end
|
585
|
+
|
586
|
+
def test_kernel_methods_can_be_mocked
|
587
|
+
m = FlexMock.new("m")
|
588
|
+
m.should_receive(:mock_kernel_function).and_return(:mock)
|
589
|
+
assert_equal :mock, m.mock_kernel_function
|
590
|
+
end
|
591
|
+
|
592
|
+
def test_undefing_kernel_methods_dont_effect_other_mocks
|
593
|
+
m = FlexMock.new("m")
|
594
|
+
m2 = FlexMock.new("m2")
|
595
|
+
m.should_receive(:mock_kernel_function).and_return(:mock)
|
596
|
+
assert_equal :mock, m.mock_kernel_function
|
597
|
+
assert_equal :mkf, m2.mock_kernel_function
|
598
|
+
end
|
599
|
+
|
569
600
|
def assert_failure
|
570
601
|
assert_raises(Test::Unit::AssertionFailedError) do yield end
|
571
602
|
end
|
data/test/test_stubbing.rb
CHANGED
@@ -107,6 +107,15 @@ class TestStubbing < Test::Unit::TestCase
|
|
107
107
|
def m.mock_verify() end
|
108
108
|
end
|
109
109
|
|
110
|
+
def test_not_calling_stubbed_method_is_an_error
|
111
|
+
dog = Dog.new
|
112
|
+
flexstub(dog).should_receive(:bark).once
|
113
|
+
assert_raise(Test::Unit::AssertionFailedError) {
|
114
|
+
flexstub(dog).mock_verify
|
115
|
+
}
|
116
|
+
dog.bark
|
117
|
+
end
|
118
|
+
|
110
119
|
def test_mock_is_verified_when_the_stub_is_verified
|
111
120
|
obj = Object.new
|
112
121
|
stub_proxy = flexstub(obj)
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0.1
|
|
3
3
|
specification_version: 1
|
4
4
|
name: flexmock
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.4.
|
7
|
-
date: 2006-09-
|
6
|
+
version: 0.4.1
|
7
|
+
date: 2006-09-07 00:00:00 -04:00
|
8
8
|
summary: Simple and Flexible Mock Objects for Testing
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -45,6 +45,7 @@ files:
|
|
45
45
|
- flexmock.blurb
|
46
46
|
- install.rb
|
47
47
|
- doc/releases/flexmock-0.4.0.rdoc
|
48
|
+
- doc/releases/flexmock-0.4.1.rdoc
|
48
49
|
test_files: []
|
49
50
|
|
50
51
|
rdoc_options:
|
@@ -57,6 +58,7 @@ extra_rdoc_files:
|
|
57
58
|
- README
|
58
59
|
- CHANGELOG
|
59
60
|
- doc/releases/flexmock-0.4.0.rdoc
|
61
|
+
- doc/releases/flexmock-0.4.1.rdoc
|
60
62
|
executables: []
|
61
63
|
|
62
64
|
extensions: []
|