<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom">
    <title>Between The Rails</title>
    <link rel="alternate" type="text/html" href="http://www.betweentherails.com/" />
    
    <id>tag:www.betweentherails.com,2007-10-02://1</id>
    <updated>2008-01-11T23:02:50Z</updated>
    <subtitle>Ruby, Rails, etc.</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Publishing Platform 4.01</generator>

<link rel="self" href="http://feeds.feedburner.com/betweentherails" type="application/atom+xml" /><entry>
    <title>Rake Args... The New Hottness!</title>
    <link rel="alternate" type="text/html" href="http://www.betweentherails.com/2008/01/rake-args-new-hottness.htm" />
    <id>tag:www.betweentherails.com,2008://1.7</id>

    <published>2008-01-11T19:00:00Z</published>
    <updated>2008-01-11T23:02:50Z</updated>

    <summary>A colleague of mind, Andy Kappen, just turned me onto some new hottness in Rake 0.8.0. Now, Andy doesn't have a blog, so I can't give him any link luv, but I can say that he and his crew held...</summary>
    <author>
        <name>Chris Kilmer</name>
        <uri>http://www.biego.com</uri>
    </author>
    
        <category term="Rake" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="railsrake" label="Rails Rake" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://www.betweentherails.com/">
        &lt;p&gt;A colleague of mind, Andy Kappen, just turned me onto some &lt;em&gt;new hottness&lt;/em&gt; in Rake 0.8.0.  Now, Andy doesn't have a blog, so I can't give him any link luv, but I can say that he and his crew held the #4 spot for &lt;a href="http://www.rockband.com/"&gt;Rock Band&lt;/a&gt;, so you know he's got some street cred.&lt;em&gt;  Unfortunately, a hardware failure and subsequent long turnaround time for repairs caused them to drop to the #22 spot.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Anyway, here are the Rake changes we'll focus on:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb&lt;/b&gt;&lt;/p&gt;


&lt;pre&gt;
  &lt;code class='ruby'&gt;
  class Task
    def invoke
    end
  end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;&lt;b&gt;/lib/ruby/gems/1.8/gems/rake-0.8.0/lib/rake.rb&lt;/b&gt;&lt;/p&gt;


&lt;pre&gt;
  &lt;code class='ruby'&gt;
  class Task
    def invoke(*args)
    end
  end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;See the difference?  In case you missed it, we can now call rake tasks with an arguments list (&lt;code&gt;*args&lt;/code&gt;) instead of using environment variables.&lt;/p&gt;

&lt;p&gt;Let's take a concrete example.  Say you wanted to write a rake task to parse a given set of revisions from your subversion log.  With Rake 0.7.3, the code might have looked like this:&lt;/p&gt;



&lt;pre&gt;
  &lt;code class='ruby'&gt;
  namespace :subversion do
    desc&amp;quot;Parse subversion log&amp;quot;
    task :parse_log do
      puts &amp;quot;parsing: svn log -r #{ENV['end_rev']}:#{ENV['start_rev']}&amp;quot;
    end
  end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;And you could call this task with:&lt;/p&gt;


&lt;pre code='console'&gt;
  &lt;code&gt;
  rake subversion:parse_log start_rev=200 end_rev=250
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Now, there is nothing wrong with that.  But, with the &lt;em&gt;new hottness&lt;/em&gt; that 0.8.0 and &lt;code&gt;*args&lt;/code&gt; provides, you can refactor your task to:&lt;/p&gt;



&lt;pre&gt;
  &lt;code class='ruby'&gt;
  namespace :subversion do
    desc&amp;quot;Parse subversion log&amp;quot;
    task :parse_log, :start_rev, :end_rev do |t, args|
      puts &amp;quot;parsing: svn log -r #{args.end_rev}:#{args.start_rev}&amp;quot;
    end
  end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;And call it like this:&lt;/p&gt;


&lt;pre code='console'&gt;
  &lt;code&gt;
  rake subversion:parse_log[200,250]
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;An added benefit of this &lt;em&gt;new hottness&lt;/em&gt; is that our task descriptions will list the expected variables:&lt;/p&gt;



&lt;pre class='console'&gt;
  &lt;code&gt;
  &amp;gt; rake -T
  .
  rake subversion:parse_log[start_rev,end_rev]  # Parse subversion log
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Simply sizzlin!&lt;/p&gt;
        
    &lt;img src="http://feeds.feedburner.com/~r/betweentherails/~4/T5brsG-jGRc" height="1" width="1"/&gt;</content>
</entry>

<entry>
    <title>Dude!  You're still testing the framework!</title>
    <link rel="alternate" type="text/html" href="http://www.betweentherails.com/2007/12/dude-youre-still-testing-the-f.htm" />
    <id>tag:www.betweentherails.com,2007://1.6</id>

    <published>2007-12-27T02:45:35Z</published>
    <updated>2007-12-27T02:46:36Z</updated>

    <summary>In my last post, Quit testing the framework!, I talked about my philosophy of testing without second guessing the Rails framework. I'd like to continue the conversation, this time focusing on the controller side of things. If you haven't read...</summary>
    <author>
        <name>Chris Kilmer</name>
        <uri>http://www.biego.com</uri>
    </author>
    
        <category term="Test" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="rails" label="Rails" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="rspec" label="RSpec" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="test" label="Test" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://www.betweentherails.com/">
        &lt;p&gt;In my last post, &lt;a href="http://www.betweentherails.com/2007/12/quit-testing-the-framework.htm"&gt;Quit testing the framework!&lt;/a&gt;, I talked about my philosophy of testing without second guessing the Rails framework.  I'd like to continue the conversation, this time focusing on the controller side of things.  If you haven't read the previous post, I suggest you &lt;em&gt;&lt;a href="http://www.betweentherails.com/2007/12/quit-testing-the-framework.htm"&gt;do it now&lt;/a&gt;&lt;/em&gt;.  Otherwise, you might be a tad lost.&lt;/p&gt;

&lt;p&gt;Ok, our controller: &lt;strong&gt;users_controller.rb&lt;/strong&gt;&lt;/p&gt;


&lt;pre&gt;
  &lt;code class='ruby'&gt;
  class UsersController &amp;lt; ApplicationController
    def index
    end
  
    def activate
      @user = User.find(params[:id])
      @user.activate! if @user
      render :action =&amp;gt; :index
    end
  end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;We are going to focus on testing the &lt;code&gt;activate&lt;/code&gt; action.  Specifically, I want to focus on &lt;em&gt;finding and activating&lt;/em&gt; the user.  I'll save the rest of the specs for the download.&lt;/p&gt;

&lt;p&gt;Here is what our initial specs might look like: &lt;strong&gt;users_controller_spec.rb&lt;/strong&gt;&lt;/p&gt;


&lt;pre&gt;
  &lt;code class='ruby'&gt;
  describe UsersController, &amp;quot;responding to POST /activate&amp;quot; do
    before(:each) do
      @user = User.create(:status =&amp;gt; &amp;quot;Some Status&amp;quot;)
    end
    
    def do_post
      post :activate, :id =&amp;gt; @user.id
    end
  
    it &amp;quot;should find a user&amp;quot; do
      do_post
      assigns[:user].should_not be_nil
    end
  
    it &amp;quot;should activate a user&amp;quot; do
      do_post
      assigns[:user].should be_active
    end  
  end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Let's look at the first spec.  We are creating a post to the &lt;code&gt;activate&lt;/code&gt; action and verifying that a user was actually found.  &lt;/p&gt;

&lt;p&gt;Do we actually need to verify that a &lt;code&gt;User&lt;/code&gt; was found?  Not really.  How about we &lt;em&gt;trust ActiveRecord&lt;/em&gt; and concentrate on making sure we call the proper &lt;code&gt;find&lt;/code&gt; method.  While we are at it, let's get rid of that nasty &lt;code&gt;assigns[:user]&lt;/code&gt; junk.  Personally, I think &lt;code&gt;assigns&lt;/code&gt; should be reserved for confirming that we actually set an instance variable. &lt;/p&gt;

&lt;p&gt;A little stubbing, a little mocking , a smidge of refactoring...&lt;/p&gt;



&lt;pre&gt;
  &lt;code class='ruby'&gt;
  describe UsersController, &amp;quot;responding to POST /activate&amp;quot; do
    before(:each) do
      @user = mock_model(User, :id =&amp;gt; &amp;quot;1&amp;quot;)
      @user.stub!(:activate!)
      User.stub!(:find).and_return(@user)
    end
  
    def do_post
      post :activate, :id =&amp;gt; @user.id
    end
  
    it &amp;quot;should find a user&amp;quot; do
      User.should_receive(:find).with(@user.id)
      do_post
    end
  end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Okay, that feels a little better to me.  We are just making sure that &lt;code&gt;User.find&lt;/code&gt; is being called properly.  Notice that we've stubbed &lt;code&gt;User.find&lt;/code&gt;.  As long as Rails is doing it's job, we shouldn't have to worry about testing the result of a call to &lt;code&gt;find&lt;/code&gt;.  A nice side effect is that we are no longer creating and destroy an actual database model for each spec.&lt;/p&gt;

&lt;p&gt;Now that we've focused on &lt;em&gt;finding&lt;/em&gt; the user, let's turn our attention to actually &lt;em&gt;activating&lt;/em&gt; the user.  Our initial spec was a lil' ikky.  We still had that ugly &lt;code&gt;assigns&lt;/code&gt; call.  And, besides, we've already tested the &lt;code&gt;activate!&lt;/code&gt; method back when we &lt;a href="http://www.betweentherails.com/2007/12/quit-testing-the-framework.htm"&gt;tested our model&lt;/a&gt;.  How about we just make sure that &lt;code&gt;activate!&lt;/code&gt; is called?  Sounds good to me.&lt;/p&gt;

&lt;p&gt;A bit more refactoring:&lt;/p&gt;



&lt;pre&gt;
  &lt;code class='ruby'&gt;
    it &amp;quot;should activate a user&amp;quot; do
      @user.should_receive(:activate!)
      do_post
    end
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Ahoy!  New hotness!  Just make sure that &lt;code&gt;activate!&lt;/code&gt; was called.  Leave the rest of the details to our model spec.  &lt;/p&gt;

&lt;p&gt;And our testing rundown:&lt;/p&gt;

&lt;table&gt;&lt;tr&gt;&lt;th&gt;Method&lt;/th&gt;&lt;th&gt;Tested&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;User.find was called&lt;/td&gt;&lt;td&gt;users_controller_spec.rb&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;@user.activate! was called&lt;/td&gt;&lt;td&gt;users_controller_spec.rb&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;@user.activate! implementation&lt;/td&gt;&lt;td&gt;user_spec.rb&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;@user.active?&lt;/td&gt;&lt;td&gt;user_spec.rb&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;@user.update_attribute&lt;/td&gt;&lt;td&gt;Rails framework&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;I've included the user model, controller and controller spec for your here: &lt;a href="http://www.betweentherails.com/assets/code/rails/models/user.rb"&gt;user.rb&lt;/a&gt;, &lt;a href="http://www.betweentherails.com/assets/code/rails/controllers/users_controller.rb"&gt;users_controller.rb&lt;/a&gt;, &lt;a href="http://www.betweentherails.com/assets/code/rails/rspec/users_controller_spec.rb"&gt;users_controller_spec.rb&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you'd like the user model spec, it was included in my &lt;a href="http://www.betweentherails.com/2007/12/quit-testing-the-framework.htm"&gt;previous post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As always, I welcome any and all comments.  Please see the sidebar for instructions on sending me a message.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;
        
    &lt;img src="http://feeds.feedburner.com/~r/betweentherails/~4/LTWnezjLQR4" height="1" width="1"/&gt;</content>
</entry>

<entry>
    <title>Quit testing the framework!</title>
    <link rel="alternate" type="text/html" href="http://www.betweentherails.com/2007/12/quit-testing-the-framework.htm" />
    <id>tag:www.betweentherails.com,2007://1.5</id>

    <published>2007-12-19T14:00:00Z</published>
    <updated>2007-12-19T13:51:35Z</updated>

    <summary>I think one of the things that I appreciate most about Ruby on Rails is the fact that it provides an accessible framework for testing. Test Driven Development is a snap with Rails. Unfortunately, I think the ease with which...</summary>
    <author>
        <name>Chris Kilmer</name>
        <uri>http://www.biego.com</uri>
    </author>
    
        <category term="Test" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="railstestrspec" label="Rails Test RSpec" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://www.betweentherails.com/">
        &lt;p&gt;I think one of the things that I appreciate most about Ruby on Rails is the fact that it provides an accessible framework for testing.  Test Driven Development is a  snap with Rails.  Unfortunately, I think the ease with which we can create test cases with Rails can lead to creating either &lt;em&gt;too many&lt;/em&gt; or the &lt;em&gt;wrong kind&lt;/em&gt; of tests.  Many times I see tests that are actually testing the Rails framework.  &lt;/p&gt;

&lt;p&gt;I think the easiest way to provide more detail is to work through an example.  I'll be using RSpec for the testing framework.  If you're not familiar with RSpec, you should still be able to translate the concepts with Test::Unit and Mocha (or similar stubbing/mocking framework).&lt;/p&gt;

&lt;p&gt;Let's start with a simple model: &lt;strong&gt;user.rb&lt;/strong&gt;&lt;/p&gt;


&lt;pre&gt;
  &lt;code class='ruby'&gt;
  class User &amp;lt; ActiveRecord::Base
    def activate!
      self.update_attribute(:status, &amp;quot;Active&amp;quot;)
    end

    def active?
      self.status == &amp;quot;Active&amp;quot;
    end

    def deactivate!
      self.update_attribute(:status, &amp;quot;Inactive&amp;quot;)
    end
  end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;First, let's write a spec to test the &lt;code&gt;activate!&lt;/code&gt; method.&lt;/p&gt;


&lt;pre&gt;
  &lt;code class='ruby'&gt;
  describe User do
    before(:each) do
      @user = User.new
    end  
  
    it &amp;quot;should update the user's status attribute when activating&amp;quot; do
      @user.activate!
      @user.should be_active
    end
  end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;And the log from running the test:&lt;/p&gt;


&lt;pre class="console"&gt;
  &lt;code&gt;
  SQL (0.000142)   BEGIN
  User Columns (0.002444)   SHOW FIELDS FROM `users`
  SQL (0.000159)   ROLLBACK
  SQL (0.000181)   BEGIN
  User Create (0.000406)   INSERT INTO `users` (`status`, `updated_at`, `created_at`) 
  ...
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Now, the spec looks innocent enough.  We are calling the &lt;code&gt;activate!&lt;/code&gt; method and testing that  the &lt;code&gt;status&lt;/code&gt; attribute was properly set .  Wait! Wait! Wait!  Think about that for a second... &lt;em&gt;testing that  the &lt;code&gt;status&lt;/code&gt; attribute was properly set&lt;/em&gt;...  Doesn't that mean that we are actually testing the results of &lt;code&gt;update_attribute&lt;/code&gt;?  Well, yes it does.  &lt;/p&gt;

&lt;p&gt;The thing is, &lt;code&gt;update_attribute&lt;/code&gt; is part of the Rails framework.  Do we really need to be writing &lt;em&gt;more&lt;/em&gt; tests for the Rails framework?  My opinion is no.  What we really need to verify is that &lt;code&gt;update_attribute&lt;/code&gt;  is being called with the proper values.  Let Rails handle the rest.  &lt;/p&gt;

&lt;p&gt;Let's modify our previous spec a bit:&lt;/p&gt;


&lt;pre&gt;
  &lt;code class='ruby'&gt;
    it &amp;quot;should update the user's status attribute when activating&amp;quot; do
      @user.should_receive(:update_attribute).with(:status, &amp;quot;Active&amp;quot;)
      @user.activate!
    end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Now, this change is closer to what I like to see.  We are verifying that &lt;code&gt;update_attribute&lt;/code&gt; is being called.  We are also verifying that we are asking Rails to update the &lt;code&gt;status&lt;/code&gt; attribute with the  value "Active".  That should be all we need to test.  Let Rails deal with &lt;code&gt;update_attribute&lt;/code&gt;, you've got business logic to test...&lt;/p&gt;

&lt;p&gt;A quick sidenote... Can you see a second benefit from our change?.  Here is the log of our new test:&lt;/p&gt;


&lt;pre class='console'&gt;
  &lt;code&gt;
  SQL (0.000087)   BEGIN
  User Columns (0.002240)   SHOW FIELDS FROM `users`
  SQL (0.000133)   ROLLBACK
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Did you see it?  There is no call to the database for updating the &lt;code&gt;status&lt;/code&gt; attribute.  By letting Rails handle the framework testing, we are also saving ourselves a little bit of time by &lt;em&gt;not&lt;/em&gt; accessing the database.&lt;/p&gt;

&lt;p&gt;Mmmmm.  Tasty!  But, I want more (I've got an insatiable sweet tooth)... Let's move on and test the &lt;code&gt;active?&lt;/code&gt; method.&lt;/p&gt;


&lt;pre&gt;
  &lt;code class='ruby'&gt;
  describe User do
    before(:each) do
      @user = User.new
    end  
  
    it &amp;quot;should update the user's status attribute when activating&amp;quot; do
      @user.should_receive(:update_attribute).with(:status, &amp;quot;Active&amp;quot;)
      @user.activate!
    end

    it &amp;quot;should be active if status is 'Active'&amp;quot;do
      @user.activate!
      @user.should be_active
    end
  end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Once again, this looks fairly straightforward, doesn't it?  Look again.  We are relying on the &lt;em&gt;results&lt;/em&gt; of the &lt;code&gt;activate!&lt;/code&gt; method.  Do we really want to be wasting the time it takes for Rails to call &lt;code&gt;update_attribute&lt;/code&gt; to properly set the status?  I certainly don't!&lt;/p&gt;

&lt;p&gt;We know what our method should be doing.  If &lt;code&gt;status&lt;/code&gt; is "Active", return true.  We don't need to navigate the Rails plumbing to test that.  Let's make another small change:&lt;/p&gt;


&lt;pre&gt;
  &lt;code class='ruby'&gt;
    it &amp;quot;should be active if status is 'Active'&amp;quot; do
      @user.stub!(:status).and_return(&amp;quot;Active&amp;quot;)
      @user.should be_active
    end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Ah, that's better.  We are verifying that we wrote &lt;code&gt;self.status == &amp;quot;Active&amp;quot;&lt;/code&gt; correctly.  No need to worry about &lt;code&gt;update_attribute&lt;/code&gt; or the rest of the Rails core.  We are now limiting our tests to code we wrote ourselves.  And again, we aren't hitting the database to set the user's status.  It's a win/win.&lt;/p&gt;

&lt;p&gt;Here is the rundown of where our code is tested:&lt;/p&gt;

&lt;table&gt;&lt;tr&gt;&lt;th&gt;Method&lt;/th&gt;&lt;th&gt;Tested&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;@user.activate!&lt;/td&gt;&lt;td&gt;user_spec.rb&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;@user.active?&lt;/td&gt;&lt;td&gt;user_spec.rb&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;@user.update_attribute&lt;/td&gt;&lt;td&gt;Rails framework&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;I've included the user model and spec files here: &lt;a href="http://www.betweentherails.com/assets/code/rails/models/user.rb"&gt;user.rb&lt;/a&gt;, &lt;a href="http://www.betweentherails.com/assets/code/rails/rspec/user_spec.rb"&gt;user_spec.rb&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;
        
    &lt;img src="http://feeds.feedburner.com/~r/betweentherails/~4/yQ7I3E5nfGU" height="1" width="1"/&gt;</content>
</entry>

<entry>
    <title>Loading your Capistrano 2 recipes</title>
    <link rel="alternate" type="text/html" href="http://www.betweentherails.com/2007/12/loading-your-capistrano-2-reci.htm" />
    <id>tag:www.betweentherails.com,2007://1.4</id>

    <published>2007-12-13T13:30:00Z</published>
    <updated>2007-12-13T13:58:54Z</updated>

    <summary>I love, love, love Capistrano 2. For the person in perpetual need of structure and symmetry, as I am, Cap 2 namespaces are a blessing. However, there is another feature of Cap 2 that is almost as cool as namespaces......</summary>
    <author>
        <name>Chris Kilmer</name>
        <uri>http://www.biego.com</uri>
    </author>
    
        <category term="Capistrano" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Rails" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="railscapistrano" label="Rails Capistrano" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://www.betweentherails.com/">
        &lt;p&gt;I love, love, love Capistrano 2.  For the person in perpetual need of structure and symmetry, as I am, Cap 2 namespaces are a blessing.   &lt;/p&gt;

&lt;p&gt;However, there is another feature of Cap 2 that is &lt;em&gt;almost&lt;/em&gt; as cool as namespaces... The ability to &lt;strong&gt;easily&lt;/strong&gt; load your custom recipes.  Let's look at a quick example...&lt;/p&gt;

&lt;p&gt;First, you need to setup your project for Cap 2:&lt;/p&gt;



&lt;pre class="console"&gt;
  &lt;code&gt;
  sandbox &amp;gt; capify .
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Once you've done that, you'll notice a new file in your project root named &lt;strong&gt;Capfile&lt;/strong&gt;.  The basic Capfile code is pretty simple:&lt;/p&gt;



&lt;pre&gt;
  &lt;code class='ruby'&gt;
    load 'deploy' if respond_to?(:namespace) # cap2 differentiator
    Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
    load 'config/deploy'
  &lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Notice that Cap 2 will look for recipes in each of your plugin directories.  &lt;em&gt;That's way cool&lt;/em&gt;.  Also, take note that &lt;strong&gt;config/deploy.rb&lt;/strong&gt; is the only other recipe that gets automatically loaded.&lt;/p&gt;

&lt;p&gt;Now, if we take a little jaunt in our Wayback Machine to the beginning of this blog post, you'll remember that I love, nay, &lt;strong&gt;need&lt;/strong&gt; symmetry.  I think of Capistrano recipes as remote rake tasks.  I realize that they can be so much more than that, but hey, humor me.&lt;/p&gt;

&lt;p&gt;We store custom rake tasks in &lt;code&gt;lib/tasks&lt;/code&gt;.  So why not store our recipes in &lt;code&gt;lib/recipes&lt;/code&gt;?  Why indeed!  I think we will.&lt;/p&gt;

&lt;p&gt;First let's create our Solr recipe: &lt;code&gt;lib/recipes/solr.rb&lt;/code&gt;:&lt;/p&gt;



&lt;pre&gt;
  &lt;code class='ruby'&gt;
   namespace :solr do
    desc &amp;quot;Start the solr server&amp;quot;
    task :start do
      puts &amp;quot;starting solr&amp;quot;
    end
  
    desc &amp;quot;Stop the solr server&amp;quot;
    task :stop do
      puts &amp;quot;stopping solr&amp;quot;
    end
  end 
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Next, our monit recipe: &lt;code&gt;lib/recipes/monit.rb&lt;/code&gt;:&lt;/p&gt;



&lt;pre&gt;
  &lt;code class='ruby'&gt;
  namespace :monit do
    desc &amp;quot;Start monit&amp;quot;
    task :start do
      puts &amp;quot;starting monit&amp;quot;
    end
  end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;The final piece of the puzzle involves adding a single line to our Capfile:&lt;/p&gt;



&lt;pre&gt;
  &lt;code class='ruby'&gt;
    load 'deploy' if respond_to?(:namespace) # cap2 differentiator
    Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
    load 'config/deploy'

    # Load recipes from lib
    Dir['lib/recipes/*.rb'].each { |recipe| load(recipe) }
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;All we did was mirror the directive for loading plugins with a slight twist.  Rather than looking in plugins directory, we focused our attention on &lt;code&gt;lib/recipes&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And now, for the moment of truth:&lt;/p&gt;



&lt;pre class='console'&gt;
  &lt;code&gt;
  sandbox &amp;gt; cap -T
  .
  cap monit:start          # Start monit
  cap solr:start           # Start the solr server
  cap solr:stop            # Stop the solr server
  sandbox &amp;gt; 
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Ahh!  Symmetry achieved.&lt;/p&gt;
        
    &lt;img src="http://feeds.feedburner.com/~r/betweentherails/~4/gf1y922bXHM" height="1" width="1"/&gt;</content>
</entry>

<entry>
    <title>Heckle your ActiveRecord objects</title>
    <link rel="alternate" type="text/html" href="http://www.betweentherails.com/2007/12/heckle-your-activerecord-objec.htm" />
    <id>tag:www.betweentherails.com,2007://1.2</id>

    <published>2007-12-12T08:21:39Z</published>
    <updated>2007-12-12T16:11:19Z</updated>

    <summary>I was recently watching Geoffrey Grosenbach's third PeepCode episode focused on rSpec: rSpec Controllers and Tools. At one point during the screencast, while working with Heckle, Geoffrey mentioned that it is easiest to run heckle against a single instance method...</summary>
    <author>
        <name>Chris Kilmer</name>
        <uri>http://www.biego.com</uri>
    </author>
    
        <category term="Rake" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="heckle" label="Heckle" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="rails" label="Rails" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="test" label="Test" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://www.betweentherails.com/">
        &lt;p&gt;I was recently watching Geoffrey Grosenbach's third &lt;a href="http://peepcode.com/"&gt;PeepCode&lt;/a&gt; episode focused on rSpec: &lt;a href="http://peepcode.com/products/rspec-controllers-and-tools"&gt;rSpec Controllers and Tools&lt;/a&gt;.    &lt;/p&gt;

&lt;p&gt;At one point during the screencast, while working with Heckle, Geoffrey mentioned that it is easiest to run heckle against a single instance method of your own making due to the fact that all objects that are subclassed from ActiveRecord::Base have all the AR methods globbed in.  No need to heckle Rails itself, eh? &lt;em&gt;By the way, if you are unfamiliar with Heckle, Kevin Clark has a great &lt;a href="http://glu.ttono.us/articles/2006/12/19/tormenting-your-tests-with-heckle"&gt;overview&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The thing is, I want to heckle all  &lt;strong&gt;my&lt;/strong&gt; methods at once. A little &lt;em&gt;A - B&lt;/em&gt; and a rake task is all it took.&lt;/p&gt;




&lt;pre&gt;
  &lt;code class='ruby'&gt;
    namespace :spec do
      desc &amp;quot;Heckle a class&amp;quot;
      task :heckle =&amp;gt; :environment do
        unless ENV.include?(&amp;quot;model&amp;quot;)
          raise &amp;quot;usage: rake test:heckle model=User&amp;quot;
        end

        #The user is allowed to test a single method
        #with User#activate! or similar
        inputs = ENV[&amp;quot;model&amp;quot;].split(&amp;quot;#&amp;quot;)

        model = inputs[0].capitalize
        test_prefix = &amp;quot;#{model.downcase}&amp;quot;
        klass = Object.const_get(&amp;quot;#{model}&amp;quot;)

        #First check to see if user provided
        #an instance method name. ex: User#activate!
        methods = [inputs[1]] if inputs.size == 2

        #If the user only passed in a model name,
        #we need to get the instance methods that
        #are not part of ActiveRecord::Base
        methods ||= klass.instance_methods - ActiveRecord::Base.instance_methods

        methods.each do |method|
          cmd = &amp;quot;spec spec -H #{model}##{method}&amp;quot;
          system(cmd)
        end
      end  
    end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;If you would like to heckle all the methods for an object (we'll user a User object as an example), run the following:&lt;/p&gt;



&lt;pre class="console"&gt;
  &lt;code&gt;
    &amp;gt; rake spec:heckle model=User
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;You can heckle a single method like so:&lt;/p&gt;



&lt;pre class="console"&gt;
  &lt;code&gt;
    &amp;gt; rake spec:heckle model=User#encrypt
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;You can download this task as well as it's Test::Unit counterpart here: &lt;a href="http://www.betweentherails.com/assets/code/rails/tasks/heckle.rake"&gt;heckle.rake&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The rake tasks have beend &lt;span class="caps"&gt;DRY&lt;/span&gt;ed up a little so that we can run most of the same code regardless of whether you are using rpsec or Test::Unit.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;
        
    &lt;img src="http://feeds.feedburner.com/~r/betweentherails/~4/5z51Pof7DuU" height="1" width="1"/&gt;</content>
</entry>

<entry>
    <title>Rake bootstrap:all</title>
    <link rel="alternate" type="text/html" href="http://www.betweentherails.com/2007/12/rake-bootstrapall.htm" />
    <id>tag:www.betweentherails.com,2007://1.3</id>

    <published>2007-12-11T14:00:00Z</published>
    <updated>2007-12-14T15:08:58Z</updated>

    <summary>Sometimes when I'm building a Rails application, I want to rebuild my database from scratch. No problem you say? Just provide a lil' lighting? A lil' thunder? Or perhaps: rake db:migrate VERSION=0 rake db:migrate Well, yeah. That works if you...</summary>
    <author>
        <name>Chris Kilmer</name>
        <uri>http://www.biego.com</uri>
    </author>
    
        <category term="Rake" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="railsrake" label="Rails Rake" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://www.betweentherails.com/">
        &lt;p&gt;Sometimes when I'm building a Rails application, I want to rebuild my database from scratch.  No problem you say?  Just provide a lil' lighting?  A lil' thunder?  Or perhaps:&lt;/p&gt;



&lt;pre class="console"&gt;
  &lt;code&gt;
     rake db:migrate VERSION=0
     rake db:migrate
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Well, yeah.  That works if you only need to rebuild the database &lt;em&gt;structure&lt;/em&gt;.  But I often find myself writing plenty of rake tasks to bootstrap different parts of the system.  For example:&lt;/p&gt;



&lt;pre&gt;
  &lt;code class='ruby'&gt;
  namespace :bootstrap do
    desc &amp;quot;Bootstrap a set of admin users&amp;quot;
    task :admins =&amp;gt; :environment do
      puts &amp;quot;Bootstrapping admins&amp;quot;
    end
  
    task :categories =&amp;gt; :environment do
      puts &amp;quot;Bootstrapping categories&amp;quot;
    end
  
    task :zip_codes =&amp;gt; :environment do
      puts &amp;quot;Bootstrapping zip_codes&amp;quot;
    end
  end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Now, how would I go about running all these tasks at once?  I could create an &lt;strong&gt;:all&lt;/strong&gt; task that called each of bootstrap tasks:&lt;/p&gt;



&lt;pre&gt;
  &lt;code class='ruby'&gt;
    namespace :bootstrap do
      desc &amp;quot;Call all the bootstrap tasks&amp;quot;
      task :all do
        tasks = %w[admins categories zip_codes]
        tasks.each do |task|
          Rake::Task[&amp;quot;bootstrap:#{task}&amp;quot;].invoke
        end
      end
    end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;This is fine if you only have a few tasks.  But what if you add a new bootstrap task and fail to add it to the task list in &lt;strong&gt;:all&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;My solution was to find a way to get all the tasks in a particular namespace.  After reading through the Rake  rdocs and fumbling around a bit, I finally resorted to bugging Mr. Rake himself, &lt;a href="http://onestepback.org/"&gt;Jim Weirich&lt;/a&gt;, who provided the hint I needed:&lt;/p&gt;

&lt;blockquote&gt;Task manager objects are just objects that respond to the protocol required to hold and manage tasks.  Currently, only the Rake::Application class responds to this protocol, but the task manager responsibilities were separated out into a module to make the list of required responsibilities more clear.  You can get the current rake application object via: &lt;strong&gt;Rake.application&lt;/strong&gt;&lt;/blockquote&gt;

&lt;p&gt;Ah!  &lt;strong&gt;Rake.application&lt;/strong&gt;.  That's the ticket!  So, here is what I ended up with:&lt;/p&gt;



&lt;pre&gt;
  &lt;code class='ruby'&gt;
    namespace :bootstrap do
      desc &amp;quot;Call all the bootstrap tasks&amp;quot;
      task :all do
        tasks = tasks_in_namespace(&amp;quot;bootstrap&amp;quot;)
        tasks.each do |task|
          Rake::Task[&amp;quot;#{task.name}&amp;quot;].invoke
        end
      end  
    end

   private
     def tasks_in_namespace(ns)
       #grab all tasks in the supplied namespace
       tasks = Rake.application.tasks.select { |t| t.name =~ /^#{ns}:/ }
       
       #make sure we don't include the :all task
       tasks.reject! { |t| t.name =~ /:all/ }
    end
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;You can get the sample file here: &lt;a href="http://www.betweentherails.com/assets/code/rails/tasks/bootstrap.rake"&gt;bootstrap.rake&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A little rake luv is all you need:&lt;/p&gt;



&lt;pre class="console"&gt;
  &lt;code&gt;
  &amp;gt; rake bootstrap:all
  Bootstrapping admins
  Bootstrapping categories
  Bootstrapping zip_codes
  &lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;Enjoy!&lt;/p&gt;
        
    &lt;img src="http://feeds.feedburner.com/~r/betweentherails/~4/_U3a__7BJHI" height="1" width="1"/&gt;</content>
</entry>

</feed>
