Ruby Style Isolating – Aspect Faking
I have talked in the past about Ruby Style Isolating (Dynamically Typed), now it is part of the AAA syntax.
The big value of this feature is that you don’t have to inherit a type in order to replace it with a fake, the downfall of this is that when you refactor your code, you might fail the test.
There are many uses. One example is if you have a class without virtual methods, you can still overload it and swap the real type with the fake one.
This gives you testability strengths and the ability to keep your design intact.
Example Suppose we have the following class:
public class NiceClassWithoutVirtual { public int NonVirtualMethod() { return 30; // an example not virtual } }
Now we want to use a manual Stub to replace our NonVirtualMethod: we can do the following: note the new before the method.
public class FakeNiceClass: NiceClassWithoutVirtual { public new int NonVirtualMethod() { return 2; // our fake value in new not virtual method } }
We can use the Stub as follows
var real = new NiceClassWithoutVirtual(); var fake = new FakeNiceClass(); Isolate.Swap.CallsOn(real).WithCallsTo(fake);
All real.NonVirtualMethod() calls will be directed to fake.NonVirtualMethod()
Fake an Aspect
Note that any methods that are not defined in the swapped type will be run as normal. This allows ‘Aspects’ of the original class to be faked. It is possible to fake a group of methods that have a specific meaning, for example just the ISerializable aspect or just the IEnumerable aspect – so we can fake the collection behavior aspect of a type. This is exactly how we implemented the Swap-Collections Feature.
Recent Posts
- Top 5 questions to ask when checking references
- Unacceptable: Unit testing will take 20 years to catch on
- The 4 reasons why we DIDN’T choose Oslo
- Typemock Academy Launch
- The First Rule to Software Craftsmanship
Categories
- .NET Tests
- Agile
- Code Integrity
- Community
- Debugging
- Fun
- Management for Geeks
- Marketing
- Product
- Release
- Reviews
- SharePoint
- TDD
- Time Management
- Uncategorized
- Unit Tests
Archives
- August 2010
- May 2010
- April 2010
- March 2010
- February 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- December 2008
- November 2008
- August 2008
- July 2008
- May 2008
- April 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
