[Rspec-devel] Rspec/Test::Unit Integration

aslak hellesoy aslak.hellesoy at gmail.com
Tue Jul 4 23:37:50 EDT 2006

On 7/4/06, Brian Takita <brian.takita at gmail.com> wrote:
> Hello,
> I had a conversation with David at the Rails Conference about integrating
> Rspec into Test::Unit.
> I want to start using Rspec at my workplace. We have alot of code in
> descendants of Test::Unit::TestCase as well as several libraries that extend
> its behaviour.
> An obstacle I ran into is the Rspec DSL is an evolutionary improvement over
> Test::Unit (i.e. you can make the same assertions and format your tests to
> look like "specs"). This being the case, I don't have a compelling reason to
> go through the effort to port all of our tests into specs.

Have you tried test2spec?

This works fairly well with 'vanilla' Test::Unit. For Rails tests,
also see the test2spec tweaks for rails:

> If Rspec where to user Test::Unit::TestCase to make its assertions, it would
> be easier to transition into using Rspec from Test::Unit.
> Anyways, I did a little proof of concept with the patch attached to this
> email.
> The example is at the bottom of this email. Rails fixtures would work in
> this case.

Rails fixtures also work with RSpec on Rails.

>From what I understand this patch allows you to use contexts inside
Test::Unit::TestCase classes.

Are you trying to work on existing Test::Unit::TestCase classes and
keep them a mix of both test methods and contexts/specs?

If you really want to stick to the Test::Unit structure, how about this:

require 'test/unit'
require 'spec'

class MyTest < Test::Unit::TestCase
  def test_should_allow_shoulds_in_tests
    (1+2).should_equal 3

My point here is that using context and specify inside a
Test::Unit::TestCase doesn't really buy you that much. Sure, the
context/specify DSL looks nice, but the real value of this DSL (IMO)
is the integration with the spec command line and the ability to
output specdocs with --format specdoc.
When things are inside a Test::Unit::TestCase you still have to use
the Test::Unit runner, and you won't get the spec commandline goodies.

The second value - being able to do myobject.should_* is easily
achievable as shown in the example above.

Or am I missing something?


> Thanks,
> Brian Takita
> require File.dirname(__FILE__) + '/../lib/spec'
> require 'test/unit'
> class RspecIntegrationTest < Test::Unit::TestCase
>   extend Spec::Runner::SpecTestCase
>   def setup
>     @test_case_setup_called = true
>   end
>   def teardown
>     puts "Test::Unit::TestCase#teardown is called too"
>   end
>   def test_something
>   end
>   def helper_method
>     @helper_method_called = true
>   end
>   context "Rspec should integrate with Test::Unit::TestCase" do
>     setup do
>       @rspec_setup_called = true
>     end
>     specify "TestCase#setup should be called." do
>       @rspec_setup_called.should_be true
>       @test_case_setup_called.should_be true
>     end
>     specify "Rspec should be able to access TestCase methods" do
>       helper_method
>       @helper_method_called.should_be true
>     end
>   end
> end
> _______________________________________________
> Rspec-devel mailing list
> Rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel

More information about the Rspec-devel mailing list