Ben Biddington

Whatever it is, it's not about "coding"

Posts Tagged ‘rspec

RSpec — common base specs

leave a comment »

Sometimes I am fond of creating inheritance chains for tests, which is easy in Java or C# say, but not quite as obvious in rspec.

Define and register a base class

It just needs to inherit from Spec::Example::ExampleGroup so it’s runnable.

require 'spec/example/example_group'
class UnitTest < Spec::Example::ExampleGroup
   protected
   def you_can_call_this
   end
end

Spec::Example::ExampleGroupFactory.register(:unit_test, UnitTest)

Set the type when describing

describe 'By supplying the :type', :type => :unit_test do
    it 'this example group inherits from whatever class you supply as :type' do
        self.protected_methods.should include('you_can_call_this')
    end
end

You now have common base class to your specifications.

Advertisements

Written by benbiddington

6 November, 2010 at 13:37

Posted in development

Tagged with , ,