Permalink
Browse files

clean up some of the mocks docs

  • Loading branch information...
1 parent eaa5534 commit ac13462b538d6916e4fe73cfa71efb38afaabd1f @dchelimsky dchelimsky committed Oct 16, 2011
Showing with 38 additions and 30 deletions.
  1. +4 −0 content/css/default.css
  2. +6 −6 content/rspec-mocks/index.page
  3. +28 −24 content/rspec-mocks/message-expectations.page
View
@@ -13,6 +13,10 @@ h3 {
padding-left: 20px;
}
+h4 {
+ padding-left: 20px;
+}
+
.warning {
color: red;
}
@@ -3,14 +3,14 @@ title: rspec-mocks
order: 3
filter:
- erb
- - textile
+ - markdown
---
-h2. <%= @page.title %>
+## <%= @page.title %>
Mock objects are imitation objects that give you declarative control over their
-behaviour in the course of the execution of an example. Defining "message
-expectations":message_expectations.html and "method stubs":stubs.html on mock
+behaviour in the course of the execution of an example. Defining [message
+expectations](message-expectations.html) and [method stubs](method-stubs.html) on mock
objects allows you to specify how one object collaborates with others before
those other objects exist.
@@ -22,7 +22,7 @@ RSpec ships with a built-in mock object framework that lets you create mock
objects in your examples or add mock-object-like behaviour to your existing
objects.
-h3. Creating a mock object
+### Creating a mock object
You create a mock object with the <code>mock</code> method:
@@ -39,7 +39,7 @@ my_mock = mock(name, stubs)
As above, but allows you to set stub return values for specific messages.
-h3. Notes
+### Notes
There are many different viewpoints about the meaning of mocks and stubs. If
you are interested in learning more, here is some recommended reading:
@@ -3,16 +3,17 @@ title: message expectations
order: 1
filter:
- erb
- - textile
+ - markdown
---
-h2. <%= @page.title %>
+## <%= @page.title %>
A message expectation (a.k.a. mock method) is an expectation that an object
should receive a specific message during the execution of an example.
-h3. Expecting Messages
+## expecting messages
+<br>
<% coderay do -%>
my_mock.should_receive(<message>)
<% end -%>
@@ -28,8 +29,9 @@ my_mock.should_not_receive(:msg)
This fails if :msg is ever received.
-h3. Expecting Arguments
+## expecting arguments
+<br>
<% coderay do -%>
my_mock.should_receive(:msg).with(<args>)
my_mock.should_receive(:msg).once.with(<args>)
@@ -59,28 +61,28 @@ Any arguments (and any number of arguments) are to be accepted. This includes
cases where no arguments are provided. *This is the default when no <code>with()</code>
clause is specified.* Even so, sometimes you want to be explicit about it.
-h3. Argument Constraints
+## argument constraints
Constraints can be placed on individual arguments which are looser than value
equivalence (as above).
-h4. anything()
+### anything()
accepts any value for this argument, e.g.:
<% coderay do -%>
my_mock.should_receive(:msg).with(1, anything(), "A")
<% end -%>
-h4. an_instance_of(<Class>)
+### an_instance_of(<Class>)
accepts any numeric value for this argument, e.g.:
<% coderay do -%>
my_mock.should_receive(:msg).with(a, an_instance_of(Fixnum), "b")
<% end -%>
-h4. hash_including(<hash>)
+### hash_including(<hash>)
accepts a hash (with or without curly braces) representing part of or the entire expected hash, e.g.:
@@ -103,15 +105,15 @@ my_mock.msg(a, 'b', :c = 'e')
my_mock.msg(a, 'b', :f = 'd')
<% end -%>
-h4. boolean()
+### boolean()
accepts a boolean value for this argument, e.g.:
<% coderay do -%>
my_mock.should_receive(:msg).with(a, boolean(), "b")
<% end -%>
-h4. duck_type(message(s))
+### duck_type(message(s))
accepts any object that responds to the prescribed message(s), e.g.:
@@ -120,7 +122,7 @@ accepts any object that responds to the prescribed message(s), e.g.:
my_mock.should_receive(:msg).with(a, duck_type(:abs, :div), "b")
<% end -%>
-h4. /regular expressions/
+### /regular expressions/
matches a String against a regular expression. If a regular expression is submitted, compares the two expressions for equality
@@ -133,12 +135,12 @@ my_mock.msg "def" #fails
my_mock.msg /bcde/ #fails
<% end -%>
-h3. Receive Counts
+## receive counts
The implicit expectation is that the message passed to <code>should_receive</code> will
be called once. You can make the expected counts explicit using the following:
-h4. Precise Counts
+### precise counts
<% coderay do -%>
my_mock.should_receive(:msg).once
@@ -158,7 +160,7 @@ my_mock.should_receive(:msg).exactly(n).times
An exception is raised if the message is received anything but <code>n</code> times.
-h4. Relative Counts - at_least
+### relative counts - at_least
<% coderay do -%>
my_mock.should_receive(:msg).at_least(:once)
@@ -178,7 +180,7 @@ my_mock.should_receive(:msg).at_least(n).times
An exception is raised if the message is received fewer than <code>n</code> times.
-h4. Relative Counts - at_most
+### relative counts - at_most
<% coderay do -%>
my_mock.should_receive(:msg).at_most(:once)
@@ -198,25 +200,25 @@ my_mock.should_receive(:msg).at_most(n).times
An exception is raised if the message is received more than <code>n</code> times (does not raise if message is never receieved).
-h4. Explicitly Imprecise Counts
+### explicitly imprecise counts
<% coderay do -%>
my_mock.should_receive(:msg).any_number_of_times
<% end -%>
The message can be received 0 or more times.
-h3. Return Values
+## return values
-h4. Single return value
+### single return value
<% coderay do -%>
my_mock.should_receive(:msg).once.and_return(<value>)
<% end -%>
Each time the expected message is received, <code>value</code> will be returned as the result.
-h4. Consecutive return values
+### consecutive return values
<% coderay do -%>
and_return(<value-1>, <value-2>, ..., <value-n>)
@@ -227,7 +229,7 @@ the result for the ith reception of the message. After the message has been
received <code>i</code> times, <code>value-n</code> is returned for all
subsequent receives.
-h4. Computed return value
+### computed return value
<% coderay do -%>
my_mock.should_receive(:msg).once.and_return {...}
@@ -242,7 +244,8 @@ values based on the arguments. For example:
my_mock.should_receive(:msg).with(instance_of(Numeric),instance_of(Numeric)).once.and_return {|a, b| a + b}
<% end -%>
-h3. Raising and Throwing
+## raising and throwing
+<br>
<% coderay do -%>
my_mock.should_receive(:msg).once.and_raise(<exception>)
@@ -259,7 +262,8 @@ my_mock.should_receive(:msg).once.and_throw(<symbol>)
Tells the mock to throw a symbol instead of returning a value.
-h3. Yielding
+## yielding
+<br>
<% coderay do -%>
my_mock.should_receive(:msg).once.and_yield(<value-1>, <value-2>, ..., <value-n>)
@@ -275,7 +279,7 @@ my_mock.should_receive(:msg).once.and_yield(<value-0-1>, <value-0-2>, ..., <valu
and_yield(<value-2-1>, <value-2-2>, ..., <value-2-n>)
<% end -%>
-h3. Ordering
+## ordering
There are times when you want to specify the order of messages sent to a mock.
It shouldn't be the case very often, but it can be handy at times.
@@ -317,7 +321,7 @@ my_mock.zero
my_mock.two
<% end -%>
-h3. Arbitrary Handling of Received Messages
+## arbitrary handling of received messages
You can supply a block to a message expectation. When the message is received
by the mock, the block is passed any arguments and evaluated. The result is

0 comments on commit ac13462

Please sign in to comment.