Skip to content

Instantly share code, notes, and snippets.

@mbj
Created November 6, 2012 23:33
Show Gist options
  • Select an option

  • Save mbj/4028478 to your computer and use it in GitHub Desktop.

Select an option

Save mbj/4028478 to your computer and use it in GitHub Desktop.
dm-2 heckle spec style helper
class Foo
def bar
:baz
end
end
# Spec in directory spec/unit/foo/bar_spec.rb
# Automagically specs Foo#bar !
unit_spec do
object_args { [] } # also empty per default
method_args { [] } # alos empty per default
# No need to define subject!
context do
idempoten_method # acts like it_should_behave_like 'an idempotent method'
#command_method # acts like it_should_behave_like 'a command method'
it 'should something' do
should be(:bar)
end
end
end
module Kernel
def unit_spec(&block)
file = caller(1).first.split(':').first
parts = file.split("/spec/unit/").last.split('/')
match = parts.last.match(/\A(.*)_spec.rb\Z/)
match || raise
parts = parts[0..-2]
singleton = false
if %w(singleton_methods class_methods).include?(parts.last)
parts = parts[0..-2]
singleton = true
end
constant = parts.map { |name| name.capitalize }.inject(Object) do |constant, name|
name = name.split('_').map(&:capitalize).join('')
constant.const_get(name)
end
delim = singleton ? '.' : '#'
method_name = match[1]
describe constant, "#{delim}#{method_name}" do
def self.method_args(&block)
let(:method_args, &block)
end
def self.attributes(&block)
let(:attributes, &block)
object_args { [attributes] }
end
def self.object_args(&block)
let(:object_args, &block)
end
def self.object(&block)
let(:object, &block)
end
def self.command_method
it_should_behave_like 'a command method'
end
def self.idempotent_method
it_should_behave_like 'an idempotent method'
end
if singleton
object { described_class }
else
object_args { [] }
object { described_class.new(*object_args) }
end
method_args { [] }
subject { object.public_send(method_name, *method_args) }
class_eval(&block)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment