Skip to content

Instantly share code, notes, and snippets.

@nazgob
Created September 30, 2012 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nazgob/3806818 to your computer and use it in GitHub Desktop.
Save nazgob/3806818 to your computer and use it in GitHub Desktop.
Sample rspec implementation from DestroyAllSoftware
require 'test/unit'
def describe(description, &block)
ExampleGroup.new(block).evaluate!
end
class ExampleGroup
def initialize(block)
@block = block
end
def evaluate!
instance_eval(&@block)
end
def it(description, &block)
block.call
end
end
class Object
def should
DelayedAssertion.new(self)
end
end
class DelayedAssertion
def initialize(subject)
@subject = subject
end
def ==(other)
raise AssertionError unless @subject == other
end
end
class AssertionError < Exception
end
class TestDescribe < Test::Unit::TestCase
def test_that_it_can_pass
describe 'some thing' do
it 'has some property' do
end
end
end
def test_that_it_can_fail
assert_raise(IndexError) do
describe 'some failing thing' do
it 'fails' do
raise IndexError
end
end
end
end
end
class TestAssertion < Test::Unit::TestCase
def test_it_should_pass
2.should == 2
end
def test_it_can_fail
assert_raise(AssertionError) do
1.should == 2
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment