File: flexmock-1.3.1.rdoc

package info (click to toggle)
ruby-flexmock 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 836 kB
  • sloc: ruby: 7,572; makefile: 6
file content (171 lines) | stat: -rw-r--r-- 4,577 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
= FlexMock 1.3.1 Released

FlexMock is a flexible mocking library for use in unit testing and
behavior specification in Ruby. This release is a minor release with a
few bug fixes and some simple features.

== Changes in 1.3.1

* Removed use of assert_block (which is deprecated in MiniTest).

* Documentation fixes.

== Changes in 1.3.0

=== Features

* Add 'and' and 'on' modifiers for the RSpec spy matcher.

* Add 'and' and 'on' options to the assert_spy_called test method.

* General documentation improvement.

=== Bug Fixes

* Fix bug in should_fail test helper that was not detecting failed
  failures.

== Changes in 1.2.0

=== Features

* Add auto configure by requiring 'flexmock/rspec/configure'.

== Changes in 1.1.0

=== Features

* Add block support to pass_thru.

== Changes in 1.0.0

=== Features

* Mocks may now have a base class that limits what methods may be
  mocked. This allows early detection of outdated mock setups when the
  methods in the class are refactored.

* Spy assertions are now allowed. The verification of the calling of
  mocked methods may now be done in the "then" portion of the test,
  after the code under test has been run. This allows for much more
  natural Given/When/Then style testing.

* A custom assert method (assert_spy_called) has been added to make
  spy assertions easy when using Test::Unit or MiniTest.

* An RSpec matcher (have_received) has been added to make spy
  assertions easy when using RSpec.

=== Bug Fixes

* Now correctly handling the mocking of meta-programmed methods.

* Using the documented +singleton_methods+ method.

* Accidently trying to partial mock a regular mock is now a no-op.

== What is FlexMock?

FlexMock is a flexible framework for creating mock object for testing.
When running unit tests, it is often desirable to use isolate the
objects being tested from the "real world" by having them interact
with simplified test objects. Sometimes these test objects simply
return values when called, other times they verify that certain
methods were called with particular arguments in a particular order.

FlexMock makes creating these test objects easy.

=== Features

* Easy integration with both Test::Unit and RSpec. Mocks created with the
  flexmock method are automatically verified at the end of the test or
  example.

* A fluent interface that allows mock behavior to be specified very
  easily.

* A "record mode" where an existing implementation can record its
  interaction with a mock for later validation against a new
  implementation.

* Easy mocking of individual methods in existing, non-mock objects.

* Easy mocking of chains of method calls.

* The ability to cause classes to instantiate test instances (instead of real
  instances) for the duration of a test.

=== Example

Suppose you had a Dog object that wagged a tail when it was happy.
Something like this:

  class Dog
    def initialize(a_tail)
      @tail = a_tail
    end
    def happy
      @tail.wag
    end
  end

To test the +Dog+ class without a real +Tail+ object (perhaps because
real +Tail+ objects activate servos in some robotic equipment), you
can do something like this:

  RSpec.configure do |config|
    config.mock_with :flexmock
  end

  describe Dog do
    it "wags its tail when happy" do
      tail = flexmock("tail")
      tail.should_receive(:wag).once
      dog = Dog.new(tail)
      dog.happy
    end
  end

FlexMock will automatically verify that the mocked tail object received the
message +wag+ exactly one time. If it doesn't, the test will not pass.

Here's the same thing using the new spy support:

  describe Dog do
    it "wags its tail when happy" do
      tail = flexmock("tail")
      dog = Dog.new(tail)
      dog.happy
      tail.should have_received(:wag)
    end
  end

This style works particularly well with the rspec-given library.

  require 'rspec/given'

  describe Dog do
    context "when the dog is happy" do
      Given(:tail) { flexmock(:on, Tail) }
      Given(:dog) { Dog.new(tail) }

      When { dog.happy }

      Then { tail.should have_received(:wag) }
    end
  end

See the FlexMock documentation at http://flexmock.rubyforge.org for details on
specifying arguments and return values on mocked methods, as well as a simple
technique for mocking tail objects when the Dog class creates the tail objects
directly.

== Availability

You can make sure you have the latest version with a quick RubyGems command:

  gem install flexmock    (you may need root/admin privileges)

You will find documentation at: http://flexmock.rubyforge.org.

-- Jim Weirich