Exploration Through Example

Example-driven development, Agile testing, context-driven testing, Agile programming, Ruby, and other things of interest to Brian Marick
191.8 167.2 186.2 183.6 184.0 183.2 184.6

Mon, 12 Apr 2004

Ruby automocks

Michael Feathers asks for a way for tests to substitute their own mock classes for any class used, however indirectly, by the class under test. That would be simpler than refactoring to pass mocks around in the code under test, though perhaps, in the long run, less desirable. As Michael says, "I'm not convinced that is great thing. Abused it would just let code get uglier without bound. But, used well, it could really help people out of some jams."

I undid a bad mood this morning by hacking up something like that for Ruby. Here's an example:

require 'test/unit'
require 'automock'

module DBI
  # This is a utility used by the class under test.
  class BigHairyDatabaseConnection
    def name
      "true database"

module SeparateAppPackage
  # This class will use the big hairy database connection.
  # How to mock that out without changing this code?
  class ClassUnderTest
    def initialize
      @database = DBI::BigHairyDatabaseConnection.new

    def which_database

class TestSeparatePackage < Test::Unit::TestCase
  include SeparateAppPackage

  # Mock out the big hairy database connection.			    
  mock DBI::BigHairyDatabaseConnection

  # Here's what it gets mocked out with.
  class MockBigHairyDatabaseConnection
    def name
      "mock database"
  def test_mock
    # Notice that the ClassUnderTest magically uses the mock,
    # not the class it was coded to use.
    assert_equal("mock database",

This is just a spike, though I don't offhand see any impediments to a solid implementation. I can finish it up or hand it off if anyone would actually use it.

## Posted at 18:59 in category /ruby [permalink] [top]

About Brian Marick
I consult mainly on Agile software development, with a special focus on how testing fits in.

Contact me here: marick@exampler.com.




Agile Testing Directions
Tests and examples
Technology-facing programmer support
Business-facing team support
Business-facing product critiques
Technology-facing product critiques
Testers on agile projects

Permalink to this list


Working your way out of the automated GUI testing tarpit
  1. Three ways of writing the same test
  2. A test should deduce its setup path
  3. Convert the suite one failure at a time
  4. You should be able to get to any page in one step
  5. Extract fast tests about single pages
  6. Link checking without clicking on links
  7. Workflow tests remain GUI tests
Permalink to this list


Design-Driven Test-Driven Design
Creating a test
Making it (barely) run
Views and presenters appear
Hooking up the real GUI


Popular Articles
A roadmap for testing on an agile project: When consulting on testing in Agile projects, I like to call this plan "what I'm biased toward."

Tacit knowledge: Experts often have no theory of their work. They simply perform skillfully.

Process and personality: Every article on methodology implicitly begins "Let's talk about me."


Related Weblogs

Wayne Allen
James Bach
Laurent Bossavit
William Caputo
Mike Clark
Rachel Davies
Esther Derby
Michael Feathers
Developer Testing
Chad Fowler
Martin Fowler
Alan Francis
Elisabeth Hendrickson
Grig Gheorghiu
Andy Hunt
Ben Hyde
Ron Jeffries
Jonathan Kohl
Dave Liebreich
Jeff Patton
Bret Pettichord
Hiring Johanna Rothman
Managing Johanna Rothman
Kevin Rutherford
Christian Sepulveda
James Shore
Jeff Sutherland
Pragmatic Dave Thomas
Glenn Vanderburg
Greg Vaughn
Eugene Wallingford
Jim Weirich


Where to Find Me

Software Practice Advancement


All of 2006
All of 2005
All of 2004
All of 2003



Agile Alliance Logo