<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Object Mentor Blog: Category David's Delirium</title>
    <link>http://blog.objectmentor.com/articles/category/david</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>RSpec-0.8.0 Released</title>
      <description>&lt;p&gt;We finally released RSpec-0.8.0 today. Check out the &lt;a href="http://rspec.rubyforge.org"&gt;rspec website&lt;/a&gt; for details.&lt;/p&gt;


	&lt;p&gt;Happy Spec&amp;#8217;ing!&lt;/p&gt;</description>
      <pubDate>Wed, 28 Feb 2007 13:39:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:34cb48c0-e715-4aff-9d3e-60d395c149ea</guid>
      <author>David Chelimsky</author>
      <link>http://blog.objectmentor.com/articles/2007/02/28/rspec-0-8-0-released</link>
      <category>David's Delirium</category>
    </item>
    <item>
      <title>sinking castles</title>
      <description>I recently heard it suggested that large scale software projects should be built 3 times before releasing them:
	&lt;ul&gt;
	&lt;li&gt;once to prototype&lt;/li&gt;
		&lt;li&gt;once to prove out design&lt;/li&gt;
		&lt;li&gt;once with the intent of release.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Today I heard this quote from Monty Python:&lt;/p&gt;


	&lt;p&gt;&amp;#8220;I built a castle in the swamp and it sunk. I built a second castle and it sunk too. I built a third castle and it burned down and then sunk. But the fourth castle, Ahhhh! That one stood.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;Maybe they were on to something&amp;#8230;&lt;/p&gt;</description>
      <pubDate>Wed, 31 Jan 2007 17:54:11 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:cda6f89f-3ee8-41ab-9fc0-91d27bddfaa9</guid>
      <author>David Chelimsky</author>
      <link>http://blog.objectmentor.com/articles/2007/01/31/sinking-castles</link>
      <category>David's Delirium</category>
    </item>
    <item>
      <title>Separating setup from specify</title>
      <description>&lt;p&gt;In his blog on &lt;a href="http://blog.objectmentor.com/articles/2007/01/13/testing-guis-part-i-ror"&gt;web &lt;span class="caps"&gt;GUI&lt;/span&gt; testing&lt;/a&gt;, &lt;a href="http://objectmentor.com/omTeam/martin_r.html"&gt;Uncle Bob&lt;/a&gt; presents an example using RSpec.&lt;/p&gt;


	&lt;p&gt;There are a couple of things that I&amp;#8217;ve begun to approach a bit differently, and I&amp;#8217;d like to present them for your consideration and comment.&lt;/p&gt;


	&lt;p&gt;In my previous posts on blogs and email lists, etc, I&amp;#8217;ve talked about using setup as the context in which each spec is run. The problem that I&amp;#8217;ve come to recognize is that there are often so many variables in contexts that things can quickly get quite confusing.&lt;/p&gt;


	&lt;p&gt;So rather than using setup as the context, I&amp;#8217;ve begun to use setup to take care of what &lt;span class="caps"&gt;NEEDS&lt;/span&gt; to happen in order for the specs to be able to run, but have each specify block tell a complete story, including its own context (i.e. Givens).&lt;/p&gt;


	&lt;p&gt;To accomplish this, I like to stub methods in setup and reserve should_receive for expectations within the specs themselves. 
Of late, I&amp;#8217;ve also been commenting the specs to clarify the Given/When/Then in each spec. Given/When/Then is conceptually the same as Build/Operate/Check, but with slightly different words chosen to be a little more customer friendly:&lt;/p&gt;


	&lt;p&gt;Given preconditions
When action
Then outcome&lt;/p&gt;


	&lt;p&gt;Mock expectations become part of the Givens. This takes a minute to get used to, but becomes quite natural.&lt;/p&gt;


	&lt;p&gt;The specs end up far more verbose, but they are &lt;span class="caps"&gt;VERY&lt;/span&gt; clear and more true to the &lt;span class="caps"&gt;BDD&lt;/span&gt; triad of Given/When/Then. Here&amp;#8217;s the example with the triad commented. I&amp;#8217;m curious to hear what you think about it:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
context "Given a request to render message/list with one message the page" do
  setup do
    @message = mock "message" 
    @caller = mock "person",:null_object=&amp;gt;true
    @school = mock "school" 

    @message.stub!(:school).and_return(school)
    @message.stub!(:time).and_return(Time.parse("12/31/06"))
    @message.stub!(:caller).and_return(@caller)
    @message.stub!(:iep).and_return(true)

    @caller.stub!(:first_name).and_return("first")
    @caller.stub!(:last_name).and_return("second")

    @school.stub!(:name).and_return("school name")
    assigns[:messages]=[m]
    assigns[:message_pages] = mock "message_pages", :null_object=&amp;gt;true
  end

  specify "should show the time" do
    #given
    @message.should_receive(:time).and_return(Time.parse("1/5/07"))

    #when
    render 'message/list'

    #then    
    response.should_have_tag :td, :content=&amp;gt;"12:00 AM 1/5", :attributes=&amp;gt;{:id=&amp;gt;"time"}
  end

  specify "should show caller first and last name" do
    #given
    @caller. should_receive(:first_name).and_return("Bob")
    @caller. should_receive(:last_name).and_return("Martin")

    #when
    render 'message/list'

    #then
    response.should_have_tag :td, :content=&amp;gt;"Bob Martin", :attributes=&amp;gt;{:id=&amp;gt;"caller"}
  end

  specify "should show school name" do
    #given
    @school. should_receive(:name).and_return("Jefferson")

    #when
    render 'message/list'

    #then
    response.should_have_tag :td, :content=&amp;gt;"Jefferson", :attributes=&amp;gt;{:id=&amp;gt;"school"}
  end

  specify "should show X in the IEP field when IEP is true" do
    #given
    @message.should_receive(:iep).and_return(true)

    #when
    render 'message/list'

    #then
    response.should_have_tag :td, :content=&amp;gt;"X",:attributes=&amp;gt;{:id=&amp;gt;"iep"}
  end

  specify "should show nothing in the IEP field when IEP is false" do
    #given
    @message.should_receive(:iep).and_return(false)

    #when
    render 'message/list'

    #then
    response.should_have_tag :td, :content=&amp;gt;"",:attributes=&amp;gt;{:id=&amp;gt;"iep"}
  end
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;As I noted above, the specs become more verbose and even bear some duplication. As you may know, I have much less trouble with duplication in specs than I do with duplication in the code being specified.&lt;/p&gt;


	&lt;p&gt;Thoughts and comments are welcome.&lt;/p&gt;</description>
      <pubDate>Sat, 13 Jan 2007 23:58:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:850c20cf-3ccb-4fc0-88f1-fd86220c5711</guid>
      <author>David Chelimsky</author>
      <link>http://blog.objectmentor.com/articles/2007/01/13/separating-setup-from-specify</link>
      <category>David's Delirium</category>
    </item>
    <item>
      <title>Why play a scale when you could be playing a tune?</title>
      <description>&lt;p&gt;Many years ago I read an interview with Pat Metheny in which the interviewer asked him about how he approaches practicing scales. This is from memory from a long time ago, so forgive any misquotes (and if anyone can help me find the text of the interview I&amp;#8217;ll be glad to update this post), but he responded with something like &amp;#8220;Scales are an important part of improvising and there are elements of scales in everything we play. But practicing scales by themselves isn&amp;#8217;t that interesting. Why would you want to play a scale when you could be playing a tune?&amp;#8221;&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ve heard other musicians talk about this in terms of &amp;#8220;if you practice scales you&amp;#8217;ll play scales on the gig&amp;#8221;. I believe this to be true, and can be found manifested in the music of many who like to call themselves jazz musicians.&lt;/p&gt;


	&lt;p&gt;That always resonated with me as an aspiring musician, and has manifested itself within my sensibilities about software as craft. This relates to why I&amp;#8217;ve latched on to &lt;span class="caps"&gt;BDD&lt;/span&gt; and the surrounding conversation.&lt;/p&gt;


	&lt;p&gt;Focusing on internal state and structure in your tests is like practicing scales. The end result is often code that is clean in a vacuum, but doesn&amp;#8217;t express its intent well &amp;#8211; just like hearing that guitar player running scales up and down (again). It may use the most theoretically efficient techniques, but it fails to tell a story.&lt;/p&gt;


	&lt;p&gt;Focusing on observable behaviour in your tests is like practicing melodies. The result is that your code is more likely to express its intent. It&amp;#8217;s more likely to be just the right code for that particular requirement. Just like the improvisor who always plays exactly the right thing for that moment.&lt;/p&gt;</description>
      <pubDate>Thu, 04 Jan 2007 23:21:35 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:ade050fc-2417-4afb-9f2b-5c003c607125</guid>
      <author>David Chelimsky</author>
      <link>http://blog.objectmentor.com/articles/2007/01/04/why-play-a-scale-when-you-could-be-playing-a-tune</link>
      <category>David's Delirium</category>
    </item>
    <item>
      <title>Moisten up your tests a little</title>
      <description>&lt;p&gt;&lt;span class="caps"&gt;DRY&lt;/span&gt; (Don&amp;#8217;t Repeat Yourself) is a very useful guideline. As with all principles, however, there is a tendency to apply it as a golden rule without really thinking about why it is meaningful, and therefore not understanding when it is important to use and when it is not.&lt;/p&gt;


	&lt;p&gt;There is another principle that sometimes conflicts with &lt;span class="caps"&gt;DRY&lt;/span&gt;: Clean Code!&lt;/p&gt;


	&lt;p&gt;For example, look at these two specs and think about which is cleaner and easier to understand:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
specify "user should supply name when registering" do
  @user = User.new(:name =&amp;gt; "David")
  @registry.should_receive(:user_name).with("David")
  @user.register(@registry)
end
&lt;/code&gt;
&lt;/pre&gt;
&lt;pre&gt;
&lt;code&gt;
specify "user should supply name when registering" do
  @name = "David" 
  @user = User.new(:name =&amp;gt; @name)
  @registry.should_receive(:user_name).with(@name)
  @user.register(@registry)
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;For me, the first one is simpler. No question. There&amp;#8217;s one less line of code (that&amp;#8217;s 25% in this case), I can clearly see the name and I don&amp;#8217;t have to mentally decode the meaning of @name &amp;#8211; nor do I need to look around to see where else it might be used. Remember, temporary variables are a code smell too.&lt;/p&gt;


	&lt;p&gt;The reason that I believe &lt;span class="caps"&gt;DRY&lt;/span&gt; to be important is that when you have duplicated code you run the risk of changing it in one place and forgetting to change it in another. If you do that, you&amp;#8217;re likely to introduce bugs. That is the &lt;strong&gt;real&lt;/strong&gt; risk &amp;#8211; not the fact that you might have to type things more than once.&lt;/p&gt;


	&lt;p&gt;You could make the argument that the second example above manifests this risk. So in that case, you have to weigh clarity and DRYness and make a choice. I&amp;#8217;ll usually choose clarity in tests.&lt;/p&gt;


	&lt;p&gt;There are other examples in which I really don&amp;#8217;t think this risk is real. One is multiple calls to a no-arg method. This came up in an &lt;a href="http://rubyforge.org/pipermail/rspec-users/2007-January/000446.html"&gt;email thread on the rspec-users mailing list&lt;/a&gt;. The poster was looking for a way to avoid writing a line of code that logs in a user in many different &lt;a href="http://rspec.rubyforge.org/"&gt;rspec contexts&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;The line to avoid was &amp;#8230;&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
session[:logged_in] = true
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;... and I recommended including a module &amp;#8230;&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
module LogIn
  def log_in
    session[:logged_in] = true
  end
end

context "Given a logged in user..." do
  include LogIn
  setup do
    log_in
  end
  ...
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The poster responded that he was concerned that there is still duplication because you have to type the two statements in every context.&lt;/p&gt;


	&lt;p&gt;I see a difference between duplicated statements and duplicated calls
to a no-arg method. While typing the call to log_in may require
a few extra strokes, the meaning of it will never change in different
contexts. So the risks associated w/ code duplication are mitigated.
In fact, the real risk is that you might change its one and only
meaning, in which case you&amp;#8217;d have to look at each case and decide if
the new meaning makes sense.&lt;/p&gt;


	&lt;p&gt;That problem wouldn&amp;#8217;t go away if you could subclass contexts. In fact,
it would be more sinister because the behaviour is implicit. At least
if you look at log_in in each context you have some sense of
what it means.&lt;/p&gt;


	&lt;p&gt;In summary, I believe strongly that &lt;span class="caps"&gt;DRY&lt;/span&gt; is a very important principle. But it&amp;#8217;s not the only principle and it definitely isn&amp;#8217;t implicitly the most important principle to apply in every situation. Sometimes a little moisture can be just the thing you need to clean up your code.&lt;/p&gt;</description>
      <pubDate>Thu, 04 Jan 2007 22:36:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:bdd90511-63ac-435d-9b14-53d9cb56ee8b</guid>
      <author>David Chelimsky</author>
      <link>http://blog.objectmentor.com/articles/2007/01/04/moisten-up-your-tests-a-little</link>
      <category>David's Delirium</category>
    </item>
  </channel>
</rss>
