<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:ent="http://www.purl.org/NET/ENT/1.0/" version="2.0">
  <channel>
    <title>Curiouser and Curiouser! on activerecord</title>
    <link>http://matt.blogs.it/</link>
    <description>RSS feed for topic activerecord</description>
    <copyright>Copyright 2009 Matt Mower. Some rights reserved.</copyright>
    <generator>Squib/0.5.0.382</generator>
    <managingEditor>self@mattmower.com</managingEditor>
    <webMaster>self@mattmower.com</webMaster>
    <language>en-gb</language>
    <item>
      <title>ActiveRecord PassportControl plugin</title>
      <link>http://matt.blogs.it/entries/00002986.html</link>
      <pubDate>Thu, 18 Jun 2009 08:55:52 +0100</pubDate>
      <description>&lt;p&gt;One of the things that irritates me from time to time about testing Rails code is when you can't easily mock the object you want. It usually comes up in a controller test where you create an AR instance in your setup but, because you pass it by id into the controller, the controller retrieves a difference instance of the record.&lt;/p&gt;

&lt;p&gt;You &lt;em&gt;can&lt;/em&gt; solve this by mocking the AR model class but this can get messy. What I really wanted was a way to mock a &lt;strong&gt;record&lt;/strong&gt; rather than an &lt;strong&gt;instance&lt;/strong&gt; so I whipped up the &lt;a href="http://github.com/mmower/passport_control/tree/master"&gt;Passport Control&lt;/a&gt; plugin. This lets me write:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;setup do
  @user = User.make
  @content = @user.contents.make
  @content.at_passport_control { |content| mock( content ).foo { "foo!" }
  get :show, :id =&amp;gt; @content.id
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The call to &lt;code&gt;at_passport_control&lt;/code&gt; ensures that whenever this &lt;strong&gt;record&lt;/strong&gt; (as identified by it's &lt;code&gt;id&lt;/code&gt;) is instantiated by ActiveRecord, by whatever chain of invocations, that it will have it's &lt;code&gt;#foo&lt;/code&gt; method mocked.&lt;/p&gt;

&lt;p&gt;Comes with a &lt;code&gt;Test::Unit&lt;/code&gt; adapater and seems to work well for me. Hope you find &lt;a href="http://github.com/mmower/passport_control/tree/master"&gt;Passport Control&lt;/a&gt; useful too.&lt;/p&gt;</description>
      <guid isPermaLink="true">http://matt.blogs.it/entries/00002986.html</guid>
      <ent:cloud ent:href="http://matt.blogs.it/topics/">
      </ent:cloud>
    </item>
  </channel>
</rss>
