Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get RSpec's shared examples like behavior in Ruby Test::Unit?

Is there a plugin/extension similar to shared_examples in RSpec for Test::Unit tests?

like image 998
Sathish Avatar asked Jan 24 '13 17:01

Sathish


1 Answers

If you are using rails (or just active_support), use a Concern.

require 'active_support/concern'

module SharedTests
  extend ActiveSupport::Concern

  included do

    # This way, test name can be a string :)
    test 'banana banana banana' do
      assert true
    end

  end
end

If you're not using active_support, just use Module#class_eval.

This technique builds on Andy H.'s answer, where he points out that:

Test::Unit tests are just Ruby classes, so you can use [normal techniques] of code reuse

but because it enables the use of ActiveSupport::Testing::Declarative#test it has the advantage of not wearing out your underscore key :)

like image 150
Jared Beck Avatar answered Sep 27 '22 18:09

Jared Beck