[rspec-devel] in source unit testing

greg weber webs.dev at gmail.com
Tue Jun 5 10:23:06 EDT 2007

After spending a little time programming in D (which has built-in support
for unittests and contracts), I got to like placing unittests directly in
the source code instead of in a seperate file.  I have created a similiar
implementation that uses rspec. This is especially nice for testing module
functions or class methods- methods that do not maintain state, and thus do
not require different context setups, or classes that have very minimal
state.  It can also be nice to use when first starting to code.  To use,
simply surround the rspec 'it' method in a unittest{} block.  The unittest
block will only be ran when using rspec.  The context is automatically
recorded as self, and the method is automatically recorded as part of the

  require 'unittest'

  module Query
    def Query.compile_queries( queries )
      ('(?:' << queries.join(')|(?:') << ')').map { |q| Regexp.compile q }
    unittest{ it "should compile regular expressions" do
      compile_queries(["foo"]).should == [/(?:foo)/]
      compile_queries(["foo","BAR"]).should == [/(?:foo)|(?:BAR)/]
    end }

The output from spec -f s
- compile_queries should compile regular expressions

For distribution of the file without the unittest dependency:
    require "unittest"
  rescue LoadError # for sure no unit testing
    def unittest; end

Is there interest in adding this to rspec?  Attached is the implementation

-Greg Weber
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-devel/attachments/20070605/c9a4da56/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: unittester.rb
Type: application/x-ruby
Size: 2258 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/rspec-devel/attachments/20070605/c9a4da56/attachment.bin 

More information about the rspec-devel mailing list