cheat sheets.

$ cheat mocha
--- mocha version 1	Thu Jul 09 23:10:26 -0700 2009
+++ mocha version 3	Mon May 10 00:22:38 -0700 2010
@@ -1,63 +1,153 @@
 Mocking a class method
   product = Product.new
   Product.expects(:find).with(1).returns(product)
   assert_equal product, Product.find(1)
 
 Mocking an instance method on a real object
   product = Product.new
   product.expects(:save).returns(true)
   assert product.save
 
 Stubbing instance methods on real object
   prices = [stub(:pence => 1000), stub(:pence => 2000)]
   product = Product.new
   product.stubs(:prices).returns(prices)
   assert_equal [1000, 2000], product.prices.collect {|p| p.pence}
 
 Stubbing an instance method on all instances of a class
   Product.any_instance.stubs(:name).returns('stubbed_name')
   product = Product.new
   assert_equal 'stubbed_name', product.name
 
 Traditional mocking
   object = mock()
   object.expects(:expected_method).with(:p1, :p2).returns(:result)
   assert_equal :result, object.expected_method(:p1, :p2)
 
 Shortcuts
   object = stub(:method1 => :result1, :method2 => :result2)
   assert_equal :result1, object.method1
   assert_equal :result2, object.method2
 
 Expection Methods:
   at_least(minimum) 
     Modifies expectation so that the expected method must be called at least a
     minimum number of times.
 
   at_least_once
     Modifies expectation so that the expected method must be called at least
     once.
 
   never
     Modifies expectation so that the expected method must never be called.
 
   raises(exception = RuntimeError, message = nil)
     Modifies expectation so that when the expected method is called, it raises
     the specified exception with the specified message.
 
   returns(value)
     Modifies expectation so that when the expected method is called, it returns
     the specified value.
 
   times(range)
     Modifies expectation so that the number of calls to the expected method must
     be within a specific range.
 
   with(*arguments, &block)
     Modifies expectation so that the expected method must be called with
     specified arguments.
 
   yields(*parameters)
     Modifies expectation so that when the expected method is called, it yields
     with the specified parameters.
+    
+    
+Parameter Matches:
+==================
+
+== Not(parameter_matcher) → parameter_matcher
+
+Matches if parameter_matcher does not match.
+
+  object = mock()
+  object.expects(:method_1).with(Not(includes(1)))
+  object.method_1([0, 2, 3])
+  # no error raised
+
+  object = mock()
+  object.expects(:method_1).with(Not(includes(1)))
+  object.method_1([0, 1, 2, 3])
+  # error raised, because method_1 was not called with object not including 1
+
+== all_of(*parameter_matchers) → parameter_matcher
+
+Matches if all parameter_matchers match.
+
+  object = mock()
+  object.expects(:method_1).with(all_of(includes(1), includes(3)))
+  object.method_1([1, 3])
+  # no error raised
+
+  object = mock()
+  object.expects(:method_1).with(all_of(includes(1), includes(3)))
+  object.method_1([1, 2])
+  # error raised, because method_1 was not called with object including 1 and 3
+
+== any_of(*parameter_matchers) → parameter_matcher
+
+Matches if any parameter_matchers match.
+
+  object = mock()
+  object.expects(:method_1).with(any_of(1, 3))
+  object.method_1(1)
+  # no error raised
+
+  object = mock()
+  object.expects(:method_1).with(any_of(1, 3))
+  object.method_1(3)
+  # no error raised
+
+  object = mock()
+  object.expects(:method_1).with(any_of(1, 3))
+  object.method_1(2)
+  # error raised, because method_1 was not called with 1 or 3
+
+
+== any_parameters() → parameter_matcher
+
+Matches any parameters.
+
+  object = mock()
+  object.expects(:method_1).with(any_parameters)
+  object.method_1(1, 2, 3, 4)
+  # no error raised
+
+  object = mock()
+  object.expects(:method_1).with(any_parameters)
+  object.method_1(5, 6, 7, 8, 9, 0)
+  # no error raised
+
+
+== anything() → parameter_matcher
+
+Matches any object.
+
+  object = mock()
+  object.expects(:method_1).with(anything)
+  object.method_1('foo')
+  # no error raised
+
+== equals(value) → parameter_matcher
+
+Matches Object equalling value.
+
+  object = mock()
+  object.expects(:method_1).with(equals(2))
+  object.method_1(2)
+  # no error raised
+
+  object = mock()
+  object.expects(:method_1).with(equals(2))
+  object.method_1(3)
+  # error raised, because method_1 was not called with Object equalling 3
. o 0 (history | current )
( add new | see all )