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

David Chelimsky dchelimsky at gmail.com
Thu Jul 6 09:24:21 EDT 2006


On 7/5/06, Nick Sieger <nicksieger at gmail.com> wrote:
> My idea for Test::Unit integration is this:  don't touch the spec DSL
> itself, instead enable a mode (perhaps the default?) where executing a spec
> generates and runs a subclass of Test::Unit instead.  This in theory would
> allow you to mix "should" behavior specs with assertions inside of a
> "specify" block.  The code to do this is pretty simple:
>
> | require 'spec/version'
> | require 'spec/api'
> | require 'spec/runner'
> | require 'test/unit'
> |
> | module Spec
> |   module Runner
> |     TEST_CLASSES = []
> |     class TestunitContext < Context
> |       def initialize(name, &context_block)
> |         @test_class = Class.new(Test::Unit::TestCase)
> |         super
> |         TEST_CLASSES << @test_class
> |       end
> |
> |       def setup(&block)
> |         @test_class.send(:define_method, :setup, &block)
> |       end
> |
> |       def teardown(&block)
> |         @test_class.send(:define_method, :teardown,
> &block)
> |       end
> |
> |       def specify(spec_name, &block)
> |         @test_class.send(:define_method, "test
> #{spec_name}".to_sym, &block)
> |       end
> |     end
> |   end
> | end
> |
> | module Kernel
> |   def context(name, &block)
> |     Spec::Runner:: TestunitContext.new(name, &block)
> |   end
> | end
>
> By putting this code in a file called 'specunit.rb' (also attached) in your
> load path and requiring it instead of 'spec' in the file containing your
> specs, I get Test::Unit execution:
>
> ruby stack_spec.rb
> Loaded suite stack_spec
> Started
> ..................
> Finished in 0.006394 seconds.
>
> 18 tests, 0 assertions, 0 failures, 0 errors
>
> Again, I'm new to the list, so apologies if this sort of thing has been
> proposed before.
>
> Cheers,
> /Nick

Nick - haven't had a chance to try this, but if it's that simple and
works that cleanly I'm all for it. Thanks for the contribution.

I've been using the expectation syntax (subject.should...) in some
Test::Unit tests on a rails project I'm working on and mixing that in
w/ test/unit does work quite nicely.

Though Aslak is correct to point out that this violates our goal of
changing the language and is also slower than just running specs.

Cheers,
David


More information about the Rspec-devel mailing list