<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>Eleutian SpeakENG Development Blog - dry</title>
    <link>http://blog.eleutian.com/</link>
    <description />
    <language>en-us</language>
    <copyright>Eleutian Technology</copyright>
    <lastBuildDate>Wed, 02 Jul 2008 03:04:07 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>aaron@eleutian.com</managingEditor>
    <webMaster>aaron@eleutian.com</webMaster>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=2bacd3f9-be27-4029-87e9-1f8111a1145b</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,2bacd3f9-be27-4029-87e9-1f8111a1145b.aspx</pingback:target>
      <dc:creator>Aaron</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,2bacd3f9-be27-4029-87e9-1f8111a1145b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=2bacd3f9-be27-4029-87e9-1f8111a1145b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Mikel Lindsaar recently <a href="http://www.lindsaar.net/2008/6/24/tip-24-being-clever-in-specs-is-for-dummies">posted
a tip</a> encouraging rSpec users to not use before :each, and set up the context
in every "it" specification. 
</p>
        <p>
I'm afraid I disagree. By pushing context setup into your specifications, you're
allowing your contexts to become artificial and anemic and your specifications to
become fat and more than just specifications. 
<br /><br />
Ultimately, this means that your reports will read poorly and it will be easy to introduce
specifications in a context that do not match the others.<br /><br />
Mikel arrives at the following specs at the end of his post: 
</p>
        <pre name="code" class="ruby">describe "when not logged in" do
  it "should redirect if we are not logged in" do
    get :index
    response.should redirect_to login_path
  end
end

describe "when logged in" do
  def given_a_logged_in_user
    session[:logged_in] = true
    session[:user_id] = 99
  end

  it "should let be a success" do
    given_a_logged_in_user
    get :index
    response.should be_success
  end

  it "should render the index template" do
    given_a_logged_in_user
    get :index
    response.should render_template('people/index')
  end
end</pre>
"when logged in" is not what I would consider a valid description of Mikel's
context in these specs. I would call it something along the lines of "when visiting
the index page while logged in". *That* is the context you are specifying against.
Compare:<br /><br />
when logged in, it should render the index template<br /><br />
vs.<br /><br />
when visiting the index page while logged in, it should render the index template<br /><br />
The first is clearly missing something. Unless rendering the index template is a direct
result of just *being* logged in, the spec is flawed.<br /><br />
With that in mind, as soon as you describe your context, there's no reason to
not pull that context setup into a single before method. It forces you to use that
context in every specification contained within your describe. It also makes your
tests easier to read. You establish your context, and then you make one line specifications
against that context. 
<br /><br />
I do agree that DRY should not be taken too far in tests. Base classes, helper methods,
all that sort of thing can quickly obfuscate them, but do not forsake the context
setup. <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=2bacd3f9-be27-4029-87e9-1f8111a1145b" /></body>
      <title>Don't take RY (Repeat Yourself) in specs too far</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,2bacd3f9-be27-4029-87e9-1f8111a1145b.aspx</guid>
      <link>http://blog.eleutian.com/2008/07/02/DontTakeRYRepeatYourselfInSpecsTooFar.aspx</link>
      <pubDate>Wed, 02 Jul 2008 03:04:07 GMT</pubDate>
      <description>&lt;p&gt;
Mikel Lindsaar recently &lt;a href="http://www.lindsaar.net/2008/6/24/tip-24-being-clever-in-specs-is-for-dummies"&gt;posted
a tip&lt;/a&gt; encouraging rSpec users to not use before :each, and set up the context
in every &amp;quot;it&amp;quot; specification. 
&lt;/p&gt;
&lt;p&gt;
I&amp;#39;m afraid I disagree. By pushing context setup into your specifications, you&amp;#39;re
allowing your contexts to become artificial and anemic and your specifications to
become fat and more than just specifications. 
&lt;br /&gt;
&lt;br /&gt;
Ultimately, this means that your reports will read poorly and it will be easy to introduce
specifications in a context that do not match the others.&lt;br /&gt;
&lt;br /&gt;
Mikel arrives at the following specs at the end of his post: 
&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;describe &amp;quot;when not logged in&amp;quot; do
  it &amp;quot;should redirect if we are not logged in&amp;quot; do
    get :index
    response.should redirect_to login_path
  end
end

describe &amp;quot;when logged in&amp;quot; do
  def given_a_logged_in_user
    session[:logged_in] = true
    session[:user_id] = 99
  end

  it &amp;quot;should let be a success&amp;quot; do
    given_a_logged_in_user
    get :index
    response.should be_success
  end

  it &amp;quot;should render the index template&amp;quot; do
    given_a_logged_in_user
    get :index
    response.should render_template(&amp;#39;people/index&amp;#39;)
  end
end&lt;/pre&gt;
&amp;quot;when logged in&amp;quot; is not what I would consider a valid description of Mikel&amp;#39;s
context in these specs. I would call it something along the lines of &amp;quot;when visiting
the index page while logged in&amp;quot;. *That* is the context you are specifying against.
Compare:&lt;br /&gt;
&lt;br /&gt;
when logged in, it should render the index template&lt;br /&gt;
&lt;br /&gt;
vs.&lt;br /&gt;
&lt;br /&gt;
when visiting the index page while logged in, it should render the index template&lt;br /&gt;
&lt;br /&gt;
The first is clearly missing something. Unless rendering the index template is a direct
result of just *being* logged in, the spec is flawed.&lt;br /&gt;
&lt;br /&gt;
With that in mind, as soon as you describe your context, there&amp;#39;s no reason to
not pull that context setup into a single before method. It forces you to use that
context in every specification contained within your describe. It also makes your
tests easier to read. You establish your context, and then you make one line specifications
against that context. 
&lt;br /&gt;
&lt;br /&gt;
I do agree that DRY should not be taken too far in tests. Base classes, helper methods,
all that sort of thing can quickly obfuscate them, but do not forsake the context
setup. &lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=2bacd3f9-be27-4029-87e9-1f8111a1145b" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,2bacd3f9-be27-4029-87e9-1f8111a1145b.aspx</comments>
      <category>bdd</category>
      <category>rspec</category>
      <category>dry</category>
    </item>
  </channel>
</rss>