How did we do that?!!
If you have read my previous post about the TypeMock Test Attributes you probably have noticed that the attributes are test framework agnostic!
So how did we do that?
We tried to add our attributes using implementing the hooks of each framework. But we found this way to be unstable:
- mbUnit is the easiest framework to extend, it is build with extensions in mind, but the extensions are version specific
- nUnit has just started supporting extensions and the system is fragile, the extension system requires deploying to each nunit installation (and TestDriven.NET too) and needs to be redistributed for each nunit version as the extensions are version specific.
- msTest (the Visual Studio Team System) currently has no means to extend and add attributes.
Then we remembered that TypeMock has an AOP framework. We can inject the Attributes! This is what we did.
We spiked and wrote the following code
[TestFixture] public class Spike { [SetUp] public void InjectVerifyMocks() { foreach (MethodInfo info in this.GetType().GetMethods()) { if (info.GetCustomAttributes(typeof(VerifyMeAttribute), false).Length > 0) { Mock m = MockManager.MockAll(this.GetType()); MethodInfo keepInfo = info; m.AlwaysReturn(info.Name, new DynamicReturnValue( delegate(object[] p, object c) { keepInfo.Invoke(c, p); MockManager.Verify(); return null; // void method })); } } } [Test] [VerifyMe] public void SpikeTest() { Mock m = MockManager.Mock<MockedClass>(); m.ExpectAndReturn("Method",10); } }
Ok, so what are we doing here?
In the setup we are scanning all the methods for a custom attribute. When we find such a method we mock ourselves!!! and change the behavior of our methods to return a dynamic return.
The dynamic return will call ourselves again!!! (via reflection) and then verify the mocks.
When we run the test (in all frameworks) it fails with VerifyException.
TestCase 'Spike.SpikeTest' failed: TypeMock.VerifyException : TypeMock Verification: Method MockedClass.Method() has 1 more expected calls
This works because while we are in the DynamicReturnValue Delegate, TypeMock is turned off and no methods are mocked. So we don’t recursively call ourselves, but on the other hand this means that our mocks are all turned off.
This was just a spike to prove that it was possible, it took more time to implemented the extension system that will work for all testing frameworks.
I will publish how to extend and create your own attributes when we release the beta version.
2 Comments to “How did we do that?!!”
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

mz6xcvavhprdpd72
[...] 1,2,3,4,5,6,7,8 Bookmark at: [...]