Flexible Mock
Hey, I've been posting too many abstract shit lately, how about some code? So here's my simple Mock class and tests for it (to show how it works). Class class Mock def initialize(method_name, block = nil ) block||=Proc.new do |*args| args=args.first if args.length==1 yield(args) end self.class.send :define_method, method_name, block end end Examples class TestMock < MiniTest::Unit::TestCase def test_mock fake = Mock.new :some_method do 'value' end assert fake.some_method == 'value' end def test_mock_single_parameter fake = Mock.new :some_method do |p| p*2 end assert_equal fake.some_method(2), 4 end def test_mock_many_parameters fake = Mock.new :some_method do |p1, p2, p3| p1*p2*p3 end assert_equal fake.some_method(2, 3, 4), 24 end def test_lambda_parameter fake = Mock.new :some_method, lambda {|p| p*2} assert_equal fake.some_method(2), 4