<?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: Separating setup from specify</title>
    <link>http://blog.objectmentor.com/articles/2007/01/13/separating-setup-from-specify</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <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>"Separating setup from specify" by Clint Shank</title>
      <description>&lt;p&gt;Hmm, this is pretty interesting.  Like Uncle Bob, I&#226;&#8364;&#8482;d like to experiment with it.  Initially, I was bothered with all the &#226;&#8364;&#339;waste&#226;&#8364;&#157; in the setup.  Now, I see what you are doing, but I was confused at first because none of the stub values were actually used in verification.  I get the NEED part now in the setup.&lt;/p&gt;


	&lt;p&gt;In this specific case, you&#226;&#8364;&#8482;re checking a small part of a single method call in each spec.  That is, did this one collaborating object get called correctly and is this one piece in the result.  That&#226;&#8364;&#8482;s what&#226;&#8364;&#8482;s new to me.  I&#226;&#8364;&#8482;d like to see how you&#226;&#8364;&#8482;d do Given/When/Then for a more object-state-focused case rather than this more result-of-a-method-call-focused case.  Perhaps you could show the spec for a Stack &#226;&#8364;&#8220; from your previous blog entry.&lt;/p&gt;</description>
      <pubDate>Thu, 18 Jan 2007 18:49:57 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:d34abd84-c262-455f-bb36-5deb9a84ead6</guid>
      <link>http://blog.objectmentor.com/articles/2007/01/13/separating-setup-from-specify#comment-38</link>
    </item>
    <item>
      <title>"Separating setup from specify" by Uncle Bob</title>
      <description>&lt;p&gt;I was skeptical of BDD at first.  My initial reaction was that it was just a small change to the syntax of the standard XUnit form; and so wasn&amp;#8217;t really significant.&lt;/p&gt;


	&lt;p&gt;However, havng used rspec for a few weeks I realized that the difference in form was changing the way I viewed the tests in a non-trivial manner.  The more I wrote tests in the BDD form, the more I wanted them to read like specs.&lt;/p&gt;


	&lt;p&gt;Making tests read like specs requires that you change the form of those tests &lt;em&gt;much&lt;/em&gt; more than the simple syntax shift from XUnit would imply.  I found myself breaking my tests up into many smaller contexts with isolated scenarios.  I found myself describing those scenarios in setup and asserting their results in  specs.  I found myself struggling to phrase the specs in a more and more declarative fashion.&lt;/p&gt;


	&lt;p&gt;This exerience has made me much less skeptical that subtle shifts in syntax can have significant effects on structure.  So I will try the Given/When/Then approach and see what happens.&lt;/p&gt;</description>
      <pubDate>Sun, 14 Jan 2007 08:29:29 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:2a1d6614-8704-494b-b098-1bf1ca6173b0</guid>
      <link>http://blog.objectmentor.com/articles/2007/01/13/separating-setup-from-specify#comment-32</link>
    </item>
  </channel>
</rss>
