While browsing the net, I found that Typemock Isolator 5.1.1 has been released, which is a mocking framework that I should try one day.
What I’m interested in is its ability to “swap” calls between real and fake objects even if they don’t implement the same interface, like this:
var duck = new Duck(); // The real, untestable class, with a Talk() method var dummy = new Dummy(); // The fake, testable class, with another Talk() method Isolate.Swap.CallsOn(duck).WithCallsTo(dummy); Assert.AreEqual("Quack", dummy.Talk()); // Then the dummy is a duck
I guess the Isolate class is “borrowed” from Ruby, but anyway, it makes testing easier.
If it walks like a duck and quacks like a duck, I would call it a duck
So, regardless of the properties and methods of both the dummy and the real duck, a dummy can be considered a duck if it satisfies the properties and methods of a real duck in the current context, or in other words; it does not have to inherit from a duck nor satisfy all behavior of a real duck, but only what we need right now [Talk()]
It is sometimes used in Static Languages, and may be more often than we expect. Here is a blog about Duck Notation in C# in which we find out that iterators in a foreach statement use duck typing. Also in BOO you can define a variable as duck.