I love \Jim Weirich’s blog\ – I believe it typifies what a blog should be, in that its not just a useless pointer at someone who points at someone else’s original content, but a content provider (for useless blogs such as this one to point at…). Rake also, I love. I’ve noted my love of his dependancy injection article. I’ve never met the man, but there is some small chance I love him as well. Yet another thing I’ve recently learnt to love is his quick and dirty \FlexMock\ test mock framework. Its a gem – try it out yourself with gem install -r FlexMock. \n\nBut one thing makes me uncomfortable with Mock frameworks: my innate, extreme stupidity. My unique ability to mock a method, then fail completely to implement it. To workaround my imbicilic nature, I’ve extended FlexMock to gently remind me to do some extra work. Here’s a bit of self-typed diff to show how its done:\n\n
\n\n#flexmock.rb, line 25-30\n- def initialize\n+ def initialize mocked_class = nil\n @handlers = Hash.new\n @counts = Hash.new(0)\n @expected_counts = Hash.new\n+ @mocked_class = mocked_class\n end\n#flexmock.rb, line 48-55\n def mock_verify\n @expected_counts.keys.each do |key|\n assert_equal @expected_counts[key], @counts[key],\n\t\"Expected method #{key} to be called #{@expected_counts[key]} times, \"\n\t\"got #{@counts[key]}\"\n+ assert @mocked_class.respond_to?( key ), \n \"Expected #{@mocked_class} to respond to method #{key}.\" unless @mocked_class.nil?\n end\n end\n\n
\n\nThis change allows me to use FlexMock in the old manner, uncaring about my stupidity (or blissfully assuming I’ll update the class later, as I bloody well should), or to use it to check my assumptions. I suspect it’d fall down if the class I was verifying was a proxy or used method_missing tricks, but then, I just wouldn’t verify that sort of class.