flexmock 0.6.3 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,5 +1,10 @@
1
1
  = Changes for FlexMock
2
2
 
3
+ == Version 0.6.4
4
+
5
+ * Renamed flexmodel(...) to flexmock(:model, ...) because visually
6
+ they were too similar.
7
+
3
8
  == Version 0.6.3
4
9
 
5
10
  * Added flexmodel() for better support of mocking ActiveRecord models.
data/README 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 :: 0.6.2.1
6
+ Version :: 0.6.4
7
7
 
8
8
  = Links
9
9
 
@@ -167,10 +167,10 @@ See FlexMock::MockContainer#flexmock for more details.
167
167
  passed to the block as an argument. Code in the block can set the desired
168
168
  expectations for the mock object.
169
169
 
170
- * <b>mock_model = flexmodel(YourModel, ...) { |mock| mock.should_receive(...) }</b>
170
+ * <b>mock_model = flexmock(:model, YourModel, ...) { |mock| mock.should_receive(...) }</b>
171
171
 
172
- The flexmodel() method will return a pure mock (not a partial mock)
173
- that will have some ActiveRecord specific methods defined.
172
+ When given :model, flexmock() will return a pure mock (not a partial
173
+ mock) that will have some ActiveRecord specific methods defined.
174
174
  YourModel should be the class of an ActiveRecord model. These
175
175
  predefined methods make it a bit easier to mock out ActiveRecord
176
176
  model objects in a Rails application. Other that the predefined
@@ -186,9 +186,6 @@ See FlexMock::MockContainer#flexmock for more details.
186
186
  * is_a?(other) -- returns true if other == YourModel.
187
187
  * class -- returns YourModel.
188
188
 
189
- The flexmodel() method is optional. You must require
190
- 'flexmock/activerecord' to enable it.
191
-
192
189
  <b>NOTE:</b> Versions of FlexMock prior to 0.6.0 used +flexstub+ to create partial mocks. The +flexmock+ method now assumes all the functionality that was spread out between two different methods. +flexstub+ is still available for backward compatibility.
193
190
 
194
191
  === Expectation Declarators
data/Rakefile CHANGED
@@ -19,7 +19,7 @@ require 'rake/contrib/rubyforgepublisher'
19
19
  CLEAN.include('*.tmp')
20
20
  CLOBBER.include("html", 'pkg')
21
21
 
22
- PKG_VERSION = '0.6.3'
22
+ PKG_VERSION = '0.6.4'
23
23
 
24
24
  PKG_FILES = FileList[
25
25
  '[A-Z]*',
@@ -1,7 +1,7 @@
1
- = FlexMock 0.6.2 Released
1
+ = FlexMock 0.6.3 Released
2
2
 
3
3
  FlexMock is a flexible mocking library for use in unit testing and behavior
4
- specification in Ruby. Version 0.6.2 introduces a two minor enhancements.
4
+ specification in Ruby. Version 0.6.3 introduces several enhancements.
5
5
 
6
6
  == New in 0.6.3
7
7
 
@@ -0,0 +1,117 @@
1
+ = FlexMock 0.6.4 Released
2
+
3
+ FlexMock is a flexible mocking library for use in unit testing and
4
+ behavior specification in Ruby. Version 0.6.4 and 0.6.3 introduce
5
+ several enhancements.
6
+
7
+ == New in 0.6.4 (and 0.6.3)
8
+
9
+ * Added the flexmock(:model, ...) method for better support when
10
+ mocking ActiveRecord objects. flexmock(:model, YourModelClass) will
11
+ return a pure mock object that responds to some basic ActiveRecord
12
+ methods with reasonable defaults.
13
+
14
+ * The flexmock() method now _always_ returns a combination domain
15
+ object / mock object. This means the object return can handle
16
+ domain methods and mock-specific methods (such as should_receive and
17
+ mock_teardown).
18
+
19
+ * A side effect of always returning a domain/mock object is that
20
+ partial mocks are now enhanced with about 5 or 6 extra methods.
21
+ Since partial mocks are real objects with just a few methods mocked,
22
+ there is a (small) potential for a method name conflict. FlexMock
23
+ now supports a safe-mode for partial mocks if this is an issue in
24
+ particular case (see the RDoc README file for more details).
25
+
26
+ * Fixed a small bug where attempting to mock a method that the partial
27
+ mock claims to respond to, but doesn't actually have defined would
28
+ cause an error. This tended to happen on active record objects
29
+ where attributes are dynamically handled.
30
+
31
+ NOTE: 0.6.4 improves the interface for mocking ActiveRecord model
32
+ objects. The flexmodel() method was too visually similar to
33
+ flexmock() and was to easy to get confused when reading code. Release
34
+ 0.6.3's flexmodel() method has been removed and a new :model mode has
35
+ been added to flexmock().
36
+
37
+ == What is FlexMock?
38
+
39
+ FlexMock is a flexible framework for creating mock object for testing. When
40
+ running unit tests, it is often desirable to use isolate the objects being
41
+ tested from the "real world" by having them interact with simplified test
42
+ objects. Sometimes these test objects simply return values when called, other
43
+ times they verify that certain methods were called with particular arguments
44
+ in a particular order.
45
+
46
+ FlexMock makes creating these test objects easy.
47
+
48
+ === Features
49
+
50
+ * Easy integration with both Test::Unit and RSpec. Mocks created with the
51
+ flexmock method are automatically verified at the end of the test or
52
+ example.
53
+
54
+ * A fluent interface that allows mock behavior to be specified very
55
+ easily.
56
+
57
+ * A "record mode" where an existing implementation can record its
58
+ interaction with a mock for later validation against a new
59
+ implementation.
60
+
61
+ * Easy mocking of individual methods in existing, non-mock objects.
62
+
63
+ * The ability to cause classes to instantiate test instances (instead of real
64
+ instances) for the duration of a test.
65
+
66
+ === Example
67
+
68
+ Suppose you had a Dog object that wagged a tail when it was happy.
69
+ Something like this:
70
+
71
+ class Dog
72
+ def initialize(a_tail)
73
+ @tail = a_tail
74
+ end
75
+ def happy
76
+ @tail.wag
77
+ end
78
+ end
79
+
80
+ To test the +Dog+ class without a real +Tail+ object (perhaps because
81
+ real +Tail+ objects activate servos in some robotic equipment), you
82
+ can do something like this:
83
+
84
+ require 'test/unit'
85
+ require 'flexmock/test_unit'
86
+
87
+ class TestDog < Test::Unit::TestCase
88
+ def test_dog_wags_tail_when_happy
89
+ tail = flexmock("tail")
90
+ tail.should_receive(:wag).once
91
+ dog = Dog.new(tail)
92
+ dog.happy
93
+ end
94
+ end
95
+
96
+ FlexMock will automatically verify that the mocked tail object received the
97
+ message +wag+ exactly one time. If it doesn't, the test will not pass.
98
+
99
+ See the FlexMock documentation at http://flexmock.rubyforge.org for details on
100
+ specifying arguments and return values on mocked methods, as well as a simple
101
+ technique for mocking tail objects when the Dog class creates the tail objects
102
+ directly.
103
+
104
+ == Availability
105
+
106
+ You can make sure you have the latest version with a quick RubyGems command:
107
+
108
+ gem install flexmock (you may need root/admin privileges)
109
+
110
+ Otherwise, you can get it from the more traditional places:
111
+
112
+ Download:: http://rubyforge.org/project/showfiles.php?group_id=170
113
+
114
+ You will find documentation at: http://flexmock.rubyforge.org.
115
+
116
+ -- Jim Weirich
117
+
@@ -114,11 +114,15 @@ class FlexMock
114
114
  quick_defs = {}
115
115
  domain_obj = nil
116
116
  safe_mode = false
117
+ model_class = nil
117
118
  while ! args.empty?
118
119
  case args.first
119
120
  when :base, :safe
120
121
  safe_mode = (args.shift == :safe)
121
122
  domain_obj = args.shift
123
+ when :model
124
+ args.shift
125
+ model_class = args.shift
122
126
  when String, Symbol
123
127
  name = args.shift.to_s
124
128
  when Hash
@@ -131,20 +135,37 @@ class FlexMock
131
135
 
132
136
  if domain_obj
133
137
  mock = flexmock_make_partial_proxy(domain_obj, name, safe_mode)
138
+ result = domain_obj
139
+ elsif model_class
140
+ id = MockContainer.next_id
141
+ result = mock = FlexMock.new("#{model_class}_#{id}")
134
142
  else
135
- mock = FlexMock.new(name || "unknown")
136
- domain_obj = mock
143
+ result = mock = FlexMock.new(name || "unknown")
137
144
  end
138
145
  flexmock_quick_define(mock, quick_defs)
139
146
  yield(mock) if block_given?
140
147
  flexmock_remember(mock)
141
- block_given? ? domain_obj : mock
142
- domain_obj
148
+ flexmock_mock_model_methods(mock, model_class, id) if model_class
149
+ result
143
150
  end
144
151
  alias flexstub flexmock
145
152
 
146
153
  private
147
154
 
155
+ # Automatically add mocks for some common methods in ActiveRecord
156
+ # models.
157
+ def flexmock_mock_model_methods(mock, model_class, id)
158
+ mock.should_receive(
159
+ :id => id,
160
+ :to_params => id.to_s,
161
+ :new_record? => false,
162
+ :class => model_class,
163
+ :errors => flexmock("errors", :count => 0))
164
+ mock.should_receive(:is_a?).with(any).and_return { |other|
165
+ other == model_class
166
+ }
167
+ end
168
+
148
169
  # Create a PartialMockProxy for the given object. Use +name+ as
149
170
  # the name of the mock object.
150
171
  def flexmock_make_partial_proxy(obj, name, safe_mode)
@@ -170,6 +191,11 @@ class FlexMock
170
191
  mocking_object.mock_container = self
171
192
  mocking_object
172
193
  end
194
+
195
+ def MockContainer.next_id
196
+ @id_counter ||= 0
197
+ @id_counter += 1
198
+ end
173
199
  end
174
200
 
175
201
  end
@@ -84,11 +84,12 @@ class FlexMock
84
84
 
85
85
  def add_mock_method(obj, method_name)
86
86
  stow_existing_definition(method_name)
87
+ eval_line = __LINE__ + 1
87
88
  eval %{
88
89
  def obj.#{method_name}(*args, &block)
89
90
  @flexmock_proxy.#{method_name}(*args, &block)
90
91
  end
91
- }
92
+ }, binding, __FILE__, eval_line
92
93
  end
93
94
 
94
95
  # :call-seq:
@@ -1,7 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'test/unit'
4
- require 'flexmock/activerecord'
5
4
 
6
5
  class DummyModel
7
6
  end
@@ -11,7 +10,7 @@ class TestFlexModel < Test::Unit::TestCase
11
10
  include FlexMock::TestCase
12
11
 
13
12
  def test_initial_conditions
14
- model = flexmodel(DummyModel)
13
+ model = flexmock(:model, DummyModel)
15
14
  assert_match(/^DummyModel_\d+/, model.mock_name)
16
15
  assert_equal model.id.to_s, model.to_params
17
16
  assert ! model.new_record?
@@ -20,21 +19,20 @@ class TestFlexModel < Test::Unit::TestCase
20
19
  end
21
20
 
22
21
  def test_mock_models_have_different_ids
23
- m1 = flexmodel(DummyModel)
24
- m2 = flexmodel(DummyModel)
22
+ m1 = flexmock(:model, DummyModel)
23
+ m2 = flexmock(:model, DummyModel)
25
24
  assert m2.id != m1.id
26
25
  end
27
26
 
28
27
  def test_mock_models_can_have_quick_defs
29
- model = flexmodel(DummyModel, :xyzzy => :ok)
28
+ model = flexmock(:model, DummyModel, :xyzzy => :ok)
30
29
  assert_equal :ok, model.xyzzy
31
30
  end
32
31
 
33
32
  def test_mock_models_can_have_blocks
34
- model = flexmodel(DummyModel) do |m|
33
+ model = flexmock(:model, DummyModel) do |m|
35
34
  m.should_receive(:xyzzy => :okdokay)
36
35
  end
37
36
  assert_equal :okdokay, model.xyzzy
38
37
  end
39
38
  end
40
-
@@ -303,6 +303,7 @@ class TestStubbing < Test::Unit::TestCase
303
303
  def test_liar_actually_lies
304
304
  liar = Liar.new
305
305
  assert liar.respond_to?(:not_defined)
306
+ assert_raise(NoMethodError) { liar.not_defined }
306
307
  end
307
308
 
308
309
  def test_partial_mock_where_respond_to_is_true_yet_method_is_not_there
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4.1
3
3
  specification_version: 1
4
4
  name: flexmock
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.6.3
7
- date: 2007-08-16 00:00:00 -04:00
6
+ version: 0.6.4
7
+ date: 2007-08-17 00:00:00 -04:00
8
8
  summary: Simple and Flexible Mock Objects for Testing
9
9
  require_paths:
10
10
  - lib
@@ -34,7 +34,6 @@ files:
34
34
  - README
35
35
  - TAGS
36
36
  - lib/flexmock.rb
37
- - lib/flexmock/activerecord.rb
38
37
  - lib/flexmock/argument_matchers.rb
39
38
  - lib/flexmock/argument_types.rb
40
39
  - lib/flexmock/base.rb
@@ -80,6 +79,7 @@ files:
80
79
  - doc/releases/flexmock-0.6.1.rdoc
81
80
  - doc/releases/flexmock-0.6.2.rdoc
82
81
  - doc/releases/flexmock-0.6.3.rdoc
82
+ - doc/releases/flexmock-0.6.4.rdoc
83
83
  test_files: []
84
84
 
85
85
  rdoc_options:
@@ -102,6 +102,7 @@ extra_rdoc_files:
102
102
  - doc/releases/flexmock-0.6.1.rdoc
103
103
  - doc/releases/flexmock-0.6.2.rdoc
104
104
  - doc/releases/flexmock-0.6.3.rdoc
105
+ - doc/releases/flexmock-0.6.4.rdoc
105
106
  executables: []
106
107
 
107
108
  extensions: []
@@ -1,27 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'flexmock'
4
-
5
- class FlexMock
6
- module MockContainer
7
- def MockContainer.next_id
8
- @id_counter ||= 0
9
- @id_counter += 1
10
- end
11
-
12
- def flexmodel(model_class, *args, &block)
13
- id = MockContainer.next_id
14
- mock = flexmock("#{model_class}_#{id}", *args, &block)
15
- mock.should_receive(
16
- :id => id,
17
- :to_params => id.to_s,
18
- :new_record? => false,
19
- :errors => flexmock("errors", :count => 0),
20
- :class => model_class)
21
- mock.should_receive(:is_a?).with(any).and_return { |other|
22
- other == model_class
23
- }
24
- mock
25
- end
26
- end
27
- end