<?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>has_many :thoughts: Start Specin' the News</title>
    <link>http://blog.kineticweb.com/articles/2007/05/27/start-specin-the-news</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Musings from a Ruby on Rails development team</description>
    <item>
      <title>Start Specin' the News</title>
      <description>&lt;p&gt;Since Colin came back from RailsConf raving about &lt;span class="caps"&gt;BDD&lt;/span&gt;, I&amp;#8217;ve found myself priming up for what previously seemed inevitable. Behavioral testing as part of a new development process. Through a better testing structure I hope to..&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;better the quality of my code&lt;/li&gt;
		&lt;li&gt;refined organization of development time&lt;/li&gt;
		&lt;li&gt;focus on the task at hand&lt;/li&gt;
		&lt;li&gt;providing well covered documentation&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;With that said&amp;#8230; and being the &lt;span class="caps"&gt;HUGE&lt;/span&gt; social loser I am&amp;#8230; I&amp;#8217;ve spent the last two days writing Specs and tests using RSpec 1.0. I have to say its almost too easy writing the actual tests, whereas the process of writing some tests, writing the code and making the code pass the test has been a definite shift in the way I develop. But not an unwelcomed one! ;)&lt;/p&gt;


	&lt;p&gt;Since RSpec seems to have a real lack of helpful documentation, I find myself wondering around their &lt;span class="caps"&gt;API&lt;/span&gt;, blog posts and best guesses. I have had great success so far, until I started writing some protected/private methods.&lt;/p&gt;


	&lt;p&gt;I had initially forseen a world of testing every single line and expression of code. Until I found this tidbit of info on the rspec mailing list&amp;#8230;&lt;/p&gt;


&lt;blockquote&gt;
1.  Should you test protected and private methods in your specs?
&lt;br /&gt;&lt;br /&gt;
No. You&amp;#8217;re specifying the behavior of an object as seen by the outside world. Of course protected and private methods aren&amp;#8217;t available to the world, so why would you need to test them? When
you&amp;#8217;re doing state-based testing, just call a method and then verify that it returns the expected value or leaves your object in an expected state.
&lt;/blockquote&gt;

	&lt;p&gt;This was backed up by almost every post after it. Seems that as a developer testing his code, I should stick with testing the main parts of the system that, overall, accomplish my task. Verifying that they are doing what I need them to do.&lt;/p&gt;


	&lt;p&gt;Private and protected methods typically back up other parts of the system that are public. For documentation purposes, I will probably write some tests that validate the functionality of a private/protected method IF they are made public. Just keeping them commented out to satisfy my entire spec.&lt;/p&gt;


	&lt;p&gt;&lt;small&gt;&lt;a href="http://rubyforge.org/pipermail/rspec-users/2007-March/001019.html"&gt;Quoted RSpec mailing list post&lt;/a&gt; and &lt;a href="http://rubyforge.org/pipermail/rspec-users/2007-March/001012.html"&gt;another great post after that&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;</description>
      <pubDate>Sun, 27 May 2007 12:57:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:fd1381b7-c3f3-4928-ad3e-73db16593f0e</guid>
      <author>Justin Reagor</author>
      <link>http://blog.kineticweb.com/articles/2007/05/27/start-specin-the-news</link>
      <category>rspec</category>
      <category>Rails</category>
      <category>bdd</category>
    </item>
    <item>
      <title>"Start Specin' the News" by Daniel Berger</title>
      <description>What was PragDave saying about Cargo Cult Programming? Don't let other people do your thinking for you. If you feel it's appropriate to test private methods, then do it.
&lt;p&gt;&lt;p&gt;
For example, I have a class that returns a struct based on a WMI object (basically, it's a Windows COM object). One of the WMI object members holds a date format that isn't understood by Date.parse, so I had to write a private method to handle it. It isn't part of the public interface, yet it doesn't belong anywhere else. I can't easily test it since the data is being returned as part of a larger structure.
&lt;p&gt;
So, I wrote a test that ensures that the private method parses the MS date format properly which is easier and better than trying to test it indirectly through the methods that call it. Furthermore, I can now copy/paste that private method where needed, secure in the knowledge that it will work properly *because I've tested it*.
&lt;p&gt;
BTW, you should be able to use Object#send to test private methods. It's what you have to do for Test::Unit. It's a minor pain (more or less intentionally), but nothing serious.
&lt;p&gt;
Dan&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;</description>
      <pubDate>Mon, 28 May 2007 11:18:35 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:188d2fc5-a266-49c1-a23d-3ccaade78e23</guid>
      <link>http://blog.kineticweb.com/articles/2007/05/27/start-specin-the-news#comment-48</link>
    </item>
    <item>
      <title>"Start Specin' the News" by Colin</title>
      <description>I would think that your protected and private methods should be tested, although indirectly. Handy tools like rcov can show you whether or not they are tested. And heckle can help you be sure they are tested well.</description>
      <pubDate>Mon, 28 May 2007 09:29:51 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:e971da03-6ff2-4093-9786-eaca4a85008a</guid>
      <link>http://blog.kineticweb.com/articles/2007/05/27/start-specin-the-news#comment-45</link>
    </item>
    <item>
      <title>"Start Specin' the News" by Mat Schaffer</title>
      <description>Also, if you can't properly exercise a private or protected method as part of a test consider testing against a dummy child class that has some of the innards exposed.  Or spin the logic out into a separate class and test that independently.  Good hunting!</description>
      <pubDate>Sun, 27 May 2007 22:31:12 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:3173e638-e5f3-49e2-be5e-b1c3e66436b5</guid>
      <link>http://blog.kineticweb.com/articles/2007/05/27/start-specin-the-news#comment-43</link>
    </item>
  </channel>
</rss>
