<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: mock code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 04 Sep 2008 20:46:06 GMT</pubDate>
    <description>DZone Snippets: mock code</description>
    <item>
      <title>Simple mock: override_method</title>
      <link>http://snippets.dzone.com/posts/show/4158</link>
      <description>Note: ALL code samples in this post were hijacked from: http://www.karmiccoding.com/articles/2006/03/11/under-the-hood-with-ruby-partial-mock-objects-for-unit-testing&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Overrides the method +method_name+ in +obj+ with the passed block&lt;br /&gt;def override_method(obj, method_name, &amp;block)&lt;br /&gt;  # Get the singleton class/eigenclass for 'obj'&lt;br /&gt;  klass = class &lt;&lt;obj; self; end &lt;br /&gt;&lt;br /&gt;  # Undefine the old method (using 'send' since 'undef_method' is protected)&lt;br /&gt;  klass.send(:undef_method, method_name)&lt;br /&gt;&lt;br /&gt;  # Create the new method&lt;br /&gt;  klass.send(:define_method, method_name, block)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Just an example class&lt;br /&gt;class Foo &lt;br /&gt;  def do_stuff&lt;br /&gt;    "I'm okay!" &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Test code&lt;br /&gt;list = []&lt;br /&gt;5.times { list.push(Foo.new) }&lt;br /&gt;&lt;br /&gt;# We override the method here!&lt;br /&gt;override_method(list.first, :do_stuff) { "I'm NOT okay!" }&lt;br /&gt;&lt;br /&gt;list.each_with_index { |f, i| puts "(#{i}) #{f.do_stuff}" }&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Outputs:&lt;br /&gt;(0) I'm NOT okay!&lt;br /&gt;(1) I'm okay!&lt;br /&gt;(2) I'm okay!&lt;br /&gt;(3) I'm okay!&lt;br /&gt;(4) I'm okay!&lt;br /&gt;</description>
      <pubDate>Mon, 18 Jun 2007 18:48:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4158</guid>
      <author>MattScilipoti (Matt Scilipoti)</author>
    </item>
    <item>
      <title>Partial Mock Object or mocking an instance of an object.</title>
      <link>http://snippets.dzone.com/posts/show/1687</link>
      <description>From: http://www.karmiccoding.com/articles/2006/03/11/under-the-hood-with-ruby-partial-mock-objects-for-unit-testing&lt;br /&gt;&lt;br /&gt;"What happens though, if you want to override a class in an instance of an object, and not all of its kind? Typically you would define a mock object, and create an instance of it. But, in Ruby there is an easier and faster way that doesn&#8217;t involve writing a different mock class for each different scenario &#8211; and it is made possible by the singleton class. This clever bit of ruby hackery lets you override the behaviour of a single instance of a class, creating what I&#8217;ve decided to call a partial mock object. To demonstrate, I&#8217;ve written a small method called override_method which will override the behaviour of the specified method in the passed object, like so:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Overrides the method +method_name+ in +obj+ with the passed block&lt;br /&gt;def override_method(obj, method_name, &amp;block)&lt;br /&gt;  # Get the singleton class/eigenclass for 'obj'&lt;br /&gt;  klass = class &lt;&lt;obj; self; end &lt;br /&gt;&lt;br /&gt;  # Undefine the old method (using 'send' since 'undef_method' is protected)&lt;br /&gt;  klass.send(:undef_method, method_name)&lt;br /&gt;&lt;br /&gt;  # Create the new method&lt;br /&gt;  klass.send(:define_method, method_name, block)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Just an example class&lt;br /&gt;class Foo &lt;br /&gt;  def do_stuff&lt;br /&gt;    "I'm okay!" &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Test code&lt;br /&gt;list = []&lt;br /&gt;5.times { list.push(Foo.new) }&lt;br /&gt;&lt;br /&gt;# We override the method here!&lt;br /&gt;override_method(list.first, :do_stuff) { "I'm NOT okay!" }&lt;br /&gt;&lt;br /&gt;list.each_with_index { |f, i| puts "(#{i}) #{f.do_stuff}" }&lt;br /&gt;&lt;br /&gt;Outputs:&lt;br /&gt;(0) I'm NOT okay!&lt;br /&gt;(1) I'm okay!&lt;br /&gt;(2) I'm okay!&lt;br /&gt;(3) I'm okay!&lt;br /&gt;(4) I'm okay!&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;As you can see, only the first object in the array&#8217;s behaviour has been changed &#8211; the rest have remained untouched. Because of this, you can embed these partial dynamic mock objects deeply into your code without the need to specially instantiate a mock object deep in your code, or writing a &#8216;clever mock&#8217; to only trigger the determined behaviour in certain objects.&lt;br /&gt;&lt;br /&gt;Where this code comes in really handy is when you need an object to raise a difficult to simulate exception (like a disk full error) on a certain method to test your error handling &#8211; simply call override_method and pass in a call to raise and voila! Dynamic partial mock objects on the fly!"&lt;br /&gt;</description>
      <pubDate>Sun, 12 Mar 2006 09:33:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1687</guid>
      <author>MattScilipoti (Matt Scilipoti)</author>
    </item>
    <item>
      <title>mocking ActiveRecord</title>
      <link>http://snippets.dzone.com/posts/show/767</link>
      <description>A search didn't turn anything up, so I tried this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;ActiveRecord::Base.class_eval do&lt;br /&gt;  alias_method :save, :valid?&lt;br /&gt;  def self.columns() @columns ||= []; end&lt;br /&gt;  &lt;br /&gt;  def self.column(name, sql_type = nil, default = nil, null = true)&lt;br /&gt;    columns &lt;&lt; ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The mock model:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;class User &lt; ActiveRecord::Base&lt;br /&gt;  validates_presence_of :login&lt;br /&gt;  column :id,       :integer&lt;br /&gt;  column :login,    :string&lt;br /&gt;  column :password, :string&lt;br /&gt;  column :active,   :boolean, true&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This should provide a quick way to test validations and things like that...&lt;br /&gt;</description>
      <pubDate>Wed, 28 Sep 2005 06:48:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/767</guid>
      <author>technoweenie (Rick Olson)</author>
    </item>
  </channel>
</rss>
