Brian Marick writes about a little-known
feature of the Ruby language: the ability to automatically mock classes
for testing at runtime. There is a keyword called "mock" that you
can use to do it. For example, if you want to temporarily replace the
behavior of a class called MyDatabaseConnector for a test, you
would use the following code:
mock MyDatabaseConnector class ClassToMockWith ..code... end
Whatever class definition directly succeeds the invocation of
"mock" will be temporarily substituted for the class
specified as an argument to "mock". Specifically, in
this case, any references to MyDatabaseConnector will be replaced with
references to ClassToMockWith, so instantiating MyDatabaseConnector
actually instantiates ClassToMockWith, and so on.
Rubyists reading this are probably thinking I’ve lost my mind,
because this isn’t really a feature of the Ruby language. It’s
just a simple (albeit powerful) extension that Brian has written. But, to
someone coming from a history of work exclusively done in in one or more of
the more static languages (Java, C#, etc.), it looks like a first-class
part of the language.
This is what Guy Steele was talking about in his Growing
a Language OOPSLA talk.