flexmock 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -1
- data/README +1 -1
- data/Rakefile +1 -1
- data/doc/releases/flexmock-0.4.2.rdoc +87 -0
- data/doc/releases/flexmock-0.4.3.rdoc +87 -0
- data/lib/flexmock.rb +3 -7
- data/test/test_stubbing.rb +8 -1
- metadata +5 -1
data/CHANGELOG
CHANGED
data/README
CHANGED
data/Rakefile
CHANGED
@@ -0,0 +1,87 @@
|
|
1
|
+
= FlexMock 0.4.2 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.2
|
8
|
+
|
9
|
+
Release 0.4.2 contains a fix for multiple definitions of a class
|
10
|
+
method stub so that the correct original definition is retained.
|
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
|
@@ -0,0 +1,87 @@
|
|
1
|
+
= FlexMock 0.4.3 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.3
|
8
|
+
|
9
|
+
Release 0.4.3 contains a fix for handling non-direct class methods
|
10
|
+
properly (bug report from Scott Barron).
|
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
@@ -971,7 +971,6 @@ class FlexMock
|
|
971
971
|
def initialize(obj, mock)
|
972
972
|
@obj = obj
|
973
973
|
@mock = mock
|
974
|
-
@methods_to_restore = []
|
975
974
|
@method_definitions = {}
|
976
975
|
@methods_proxied = []
|
977
976
|
end
|
@@ -980,9 +979,6 @@ class FlexMock
|
|
980
979
|
# mock object handle should_receive.
|
981
980
|
def should_receive(method_name)
|
982
981
|
method_name = method_name.to_sym
|
983
|
-
if @obj.methods.include?(method_name.to_s)
|
984
|
-
@methods_to_restore << method_name
|
985
|
-
end
|
986
982
|
unless @methods_proxied.include?(method_name)
|
987
983
|
hide_existing_method(method_name)
|
988
984
|
@methods_proxied << method_name
|
@@ -999,9 +995,9 @@ class FlexMock
|
|
999
995
|
# Remove all traces of the mocking framework from the existing object.
|
1000
996
|
def mock_teardown
|
1001
997
|
if ! detached?
|
1002
|
-
@
|
998
|
+
@methods_proxied.each do |method_name|
|
1003
999
|
remove_current_method(method_name)
|
1004
|
-
restore_original_definition(method_name)
|
1000
|
+
restore_original_definition(method_name)
|
1005
1001
|
end
|
1006
1002
|
@obj.instance_variable_set("@flexmock_proxy", nil)
|
1007
1003
|
@obj = nil
|
@@ -1060,7 +1056,7 @@ class FlexMock
|
|
1060
1056
|
# Remove the current method if it is a singleton method of the
|
1061
1057
|
# object being mocked.
|
1062
1058
|
def remove_current_method(method_name)
|
1063
|
-
sclass.class_eval { remove_method(method_name)
|
1059
|
+
sclass.class_eval { remove_method(method_name) }
|
1064
1060
|
end
|
1065
1061
|
|
1066
1062
|
# Have we been detached from the existing object?
|
data/test/test_stubbing.rb
CHANGED
@@ -58,7 +58,6 @@ class TestStubbing < Test::Unit::TestCase
|
|
58
58
|
stub_proxy.mock_teardown
|
59
59
|
assert_equal :woof, dog.bark
|
60
60
|
assert_equal nil, dog.instance_variable_get("@flexmock_proxy")
|
61
|
-
assert ! dog.methods.include?("flexmock_old_bark")
|
62
61
|
end
|
63
62
|
|
64
63
|
def test_original_missing_behavior_can_be_restored
|
@@ -119,6 +118,14 @@ class TestStubbing < Test::Unit::TestCase
|
|
119
118
|
assert_equal "hello3", obj.hi(3)
|
120
119
|
end
|
121
120
|
|
121
|
+
def test_original_behavior_is_restored_on_nonsingleton_methods_with_multiple_stubs
|
122
|
+
flexstub(File).should_receive(:open).with("foo").once.and_return(:ok)
|
123
|
+
flexstub(File).should_receive(:open).with("bar").once.and_return(:ok)
|
124
|
+
|
125
|
+
assert_equal :ok, File.open("foo")
|
126
|
+
assert_equal :ok, File.open("bar")
|
127
|
+
end
|
128
|
+
|
122
129
|
def test_original_behavior_is_restored_even_when_errors
|
123
130
|
flexstub(Dog).should_receive(:create).once.and_return(:mock)
|
124
131
|
flexmock_teardown rescue nil
|
metadata
CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.0.6
|
|
3
3
|
specification_version: 1
|
4
4
|
name: flexmock
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.4.
|
6
|
+
version: 0.4.3
|
7
7
|
date: 2006-10-18 00:00:00 -04:00
|
8
8
|
summary: Simple and Flexible Mock Objects for Testing
|
9
9
|
require_paths:
|
@@ -46,6 +46,8 @@ files:
|
|
46
46
|
- install.rb
|
47
47
|
- doc/releases/flexmock-0.4.0.rdoc
|
48
48
|
- doc/releases/flexmock-0.4.1.rdoc
|
49
|
+
- doc/releases/flexmock-0.4.2.rdoc
|
50
|
+
- doc/releases/flexmock-0.4.3.rdoc
|
49
51
|
test_files: []
|
50
52
|
|
51
53
|
rdoc_options:
|
@@ -59,6 +61,8 @@ extra_rdoc_files:
|
|
59
61
|
- CHANGELOG
|
60
62
|
- doc/releases/flexmock-0.4.0.rdoc
|
61
63
|
- doc/releases/flexmock-0.4.1.rdoc
|
64
|
+
- doc/releases/flexmock-0.4.2.rdoc
|
65
|
+
- doc/releases/flexmock-0.4.3.rdoc
|
62
66
|
executables: []
|
63
67
|
|
64
68
|
extensions: []
|