<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>The Napkin ~ A Blog By Highgroove Studios comments on Getting Started with Testing: Unit Testing or BDD with Rspec</title>
    <link>http://napkin.highgroove.com/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>The Napkin ~ A Blog By Highgroove Studios comments</description>
    <item>
      <title>"Getting Started with Testing: Unit Testing or BDD with Rspec": comment by bryanl</title>
      <description>&lt;p&gt;I think you can just use shoulda and skip rspec all together (well at least until they get the story stuff working correctly)&lt;/p&gt;</description>
      <pubDate>Tue,  1 Apr 2008 06:39:59 EST</pubDate>
      <guid>http://napkin.highgroove.com/articles/2008/03/31/getting-started-with-testing-unit-testing-or-bdd-with-rspec#comment-1009</guid>
      <link>http://napkin.highgroove.com/articles/2008/03/31/getting-started-with-testing-unit-testing-or-bdd-with-rspec#comment-1009</link>
    </item>
    <item>
      <title>"Getting Started with Testing: Unit Testing or BDD with Rspec" by cbq</title>
      <description>&lt;p&gt;If you&amp;#8217;re just getting started with testing (and general test-first or test-driven development) in your Ruby and Ruby on Rails applications, you have a couple of choices.&lt;/p&gt;


	&lt;p&gt;You can go with Ruby&amp;#8217;s &lt;a href="http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html"&gt;Unit::Test&lt;/a&gt;, built right in to Ruby, and built-in to Rails with unit, functional, and integration test suites setup for you.&lt;/p&gt;


	&lt;p&gt;Or, you could setup &lt;a href="http://rspec.info/"&gt;Rspec&lt;/a&gt; with &lt;a href="http://mocha.rubyforge.org/"&gt;Mocha&lt;/a&gt;, and implement a form of testing called Behavior Driven Development or &lt;span class="caps"&gt;BDD&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;Both approaches serve the same goal: better, tested code, easier code to maintain, and in general, just better practices.&lt;/p&gt;


	&lt;p&gt;My advice is to start with unit tests, and then move to Rspec later.&lt;/p&gt;

&lt;p&gt;Once you get the hang of the built-in unit testing framework, you&amp;#8217;ll start to write tests named like this:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
def test_login
 #
end

def test_create
 #
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;which is fine, and gets the job done (I do it like this!).
Eventually, after getting the hang of it, you&amp;#8217;ll start to write
shorter and shorter tests named like this:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
def test_should_allow_login
 # ...
end

def test_should_not_let_user_view_admin_page
 # ...
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;etc. etc.&lt;/p&gt;


	&lt;p&gt;And next thing you know, you&amp;#8217;re doing a form of behavior driven development, but without Rspec (I do this too&amp;#8212;and yes I know, it&amp;#8217;s not completely rspec&amp;#8212;it lacks the mocking/stubbing
non-boundary-crossing unit-test tools).  But, there&amp;#8217;s a lot more documentation and examples out there as of right now using good ole unit testing.&lt;/p&gt;


	&lt;p&gt;I forget where I read somewhere that David Heinemeier Hansson (DHH) doesn&amp;#8217;t use Rspec (yet), but writes his test with the Rspec style should and should_not methods, and tries to adhere to good, single units of functionality and behavior in tests.&lt;/p&gt;


	&lt;p&gt;If you install plugins in your Rails application, they will have their own tests, but they don&amp;#8217;t actually run in conjunction with your code.  So, while you should run a plugin&amp;#8217;s tests once you pull down the code&amp;#8212;you don&amp;#8217;t really need to run them again.  You&amp;#8217;ll be writing your own tests, that may execute parts of the plugins code, and that can be your own flavor/style.&lt;/p&gt;


	&lt;p&gt;It will feel at first like you are troubleshooting tests, along with code&amp;#8212;which is irritating, I know, but stick with it.  It takes longer in the beginning, but is well worth it, once you get going.&lt;/p&gt;


	&lt;p&gt;I recommend treating your tests like debugging statements, and using puts statements everywhere while you figure it out, so that when you run your tests (hopefully using Apple-R while inside Textmate, or using the Run-command in most other editors), you can see:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
puts "widget should have errors: #{@widget.errors.inspect}" 
assert !@widget.errors.empty?
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Of course, be sure to remove or comment out those puts statements before you check in your tests.  You can always uncomment them for debugging later.&lt;/p&gt;


	&lt;p&gt;A former Big Nerd Ranch alumni says:&lt;/p&gt;


&lt;blockquote&gt;
A great tool to help migrate between Test::Unit and RSpec / &lt;span class="caps"&gt;BDD&lt;/span&gt; is the plugin  &lt;a href="http://www.thoughtbot.com/projects/shoulda"&gt;Shoulda&lt;/a&gt;.

It looks a lot like RSpec / Test::Spec but it is all just decoration on top of Test::Unit.  You get a lot of really nice tools like nested contexts, which help hierarchically structure your tests, and some excellent ActiveRecord association and validation tests.
&lt;/blockquote&gt;</description>
      <pubDate>Mon, 31 Mar 2008 08:28:00 EST</pubDate>
      <guid>&lt;a href="/articles/2008/03/31/getting-started-with-testing-unit-testing-or-bdd-with-rspec"&gt;Getting Started with Testing: Unit Testing or BDD with Rspec&lt;/a&gt;</guid>
      <link>&lt;a href="/articles/2008/03/31/getting-started-with-testing-unit-testing-or-bdd-with-rspec"&gt;Getting Started with Testing: Unit Testing or BDD with Rspec&lt;/a&gt;</link>
    </item>
  </channel>
</rss>
