<?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>David Chelimsky: Spec::Expectations and Test::Unit::TestCase, together again at last</title>
    <link>http://blog.davidchelimsky.net/articles/2007/03/01/spec-expectations-and-test-unit-testcase-together-again-at-last</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>on software in process and practice</description>
    <item>
      <title>Spec::Expectations and Test::Unit::TestCase, together again at last</title>
      <description>&lt;p&gt;In &lt;a href="http://www.robsanheim.com/"&gt;Rob Sanheim&amp;rsquo;s&lt;/a&gt; &lt;a href="http://www.robsanheim.com/2006/12/29/bdd-in-rails-testspec-and-rspec/"&gt;blog on comparing test/spec w/ rspec&lt;/a&gt;, Rob pointed out that he had &amp;#8220;been following RSpec, the better known Ruby &lt;span class="caps"&gt;BDD&lt;/span&gt; library for awhile, but decided against it since it just doesn&amp;#8217;t look practical for use in an established project with around ~400 test cases.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;As it turns out, rspec-0.8 has done a much better job of isolating components. It&amp;#8217;s not quite ideal yet, but it is sufficient to support using RSpec&amp;#8217;s expectations right in your Test::Unit::TestCases.&lt;/p&gt;&lt;p&gt;To make this happen, you need to require a few things:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;require 'test/unit'
require 'rubygems'
gem 'rspec'
require 'spec/expectations'
require 'spec/matchers'&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;&amp;#8216;spec/expectations&amp;#8217; adds #should and #should_not to your objects. &amp;#8216;spec/matchers&amp;#8217; provides RSpec&amp;#8217;s Expression Matchers, which you then need to explicitly include in the TestCase:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class ThingTest &amp;lt; Test::Unit::TestCase
  include Spec::Matchers
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Here&amp;#8217;s an example with one passing and one failing test.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;require 'test/unit'
require 'rubygems'
gem 'rspec'
require 'spec/expectations'
require 'spec/matchers'

class ThingTest &amp;lt; Test::Unit::TestCase
  include Spec::Matchers

  def setup
    @thing = Thing.new
  end

  def test_should_have_4_subthings #should fail
    @thing.should have(4).sub_things
  end

  def test_should_have_3_subthings #should pass
    @thing.should have(3).sub_things
  end
end

class Thing
  def sub_things
    [1,2,3]
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Assuming that you have rspec-0.8.0 or better, this should produce the following output:&lt;/p&gt;


&lt;pre&gt;$ ruby thing_test.rb 
Loaded suite thing_test
Started
.E
Finished in 0.000642 seconds.

  1) Error:
test_should_have_4_subthings(ThingTest):
Spec::Expectations::ExpectationNotMetError: expected 4 sub_things, got 3
    /usr/local/lib/ruby/gems/1.8/gems/rspec-0.8.1/lib/spec/expectations.rb:55:in `fail_with'
    /usr/local/lib/ruby/gems/1.8/gems/rspec-0.8.1/lib/spec/expectations/handler.rb:17:in `handle_matcher'
    /usr/local/lib/ruby/gems/1.8/gems/rspec-0.8.1/lib/spec/expectations/extensions/object.rb:28:in `should'
    thing_test.rb:15:in `test_should_have_4_subthings'

2 tests, 0 assertions, 0 failures, 1 errors
&lt;/pre&gt;

	&lt;p&gt;So now, although we&amp;#8217;d like to see people who want to use RSpec using RSpec, this should lower the barrier to those who wish to migrate existing systems gradually.&lt;/p&gt;</description>
      <pubDate>Thu, 01 Mar 2007 10:58:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:12aa374d-d03a-4690-b533-a833c1c9b7c4</guid>
      <author>David</author>
      <link>http://blog.davidchelimsky.net/articles/2007/03/01/spec-expectations-and-test-unit-testcase-together-again-at-last</link>
      <category>rspec</category>
      <category>test_unit</category>
      <trackback:ping>http://blog.davidchelimsky.net/articles/trackback/51</trackback:ping>
    </item>
    <item>
      <title>"Spec::Expectations and Test::Unit::TestCase, together again at last" by David Chelimsky</title>
      <description>&lt;p&gt;FYI &amp;#8211; I&amp;#8217;ve added an easier way to do this to the soon-to-be-released rspec-0.9&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;require 'test/unit'
require 'spec/test_case_adapter'

class MyTest &amp;lt; Test::Unit::TestCase
  #...
end
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Thu, 29 Mar 2007 07:43:36 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:2536c723-ac2e-4c63-90de-46536c68bdd8</guid>
      <link>http://blog.davidchelimsky.net/articles/2007/03/01/spec-expectations-and-test-unit-testcase-together-again-at-last#comment-216</link>
    </item>
    <item>
      <title>"Spec::Expectations and Test::Unit::TestCase, together again at last" by Rob Sanheim</title>
      <description>&lt;p&gt;Thanks for writing about this, David.  Its nice to see that rspec is becoming friendlier for &amp;#8220;legacy&amp;#8221; rails projects.  We have enough code using test/spec now that I&amp;#8217;m definitely not going to rewrite existing projects using it, but I&amp;#8217;ll certainly consider rspec for new projects.  Now, if only mocha would place nice with rspec&amp;#8230; =)&lt;/p&gt;</description>
      <pubDate>Wed, 14 Mar 2007 16:49:44 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:3624cde2-3ee3-48dc-8fc6-9494bffbb05f</guid>
      <link>http://blog.davidchelimsky.net/articles/2007/03/01/spec-expectations-and-test-unit-testcase-together-again-at-last#comment-89</link>
    </item>
  </channel>
</rss>
