How To Isolate Future Instances
One of the benifits of testing with TypeMock is the ability to mock future objects. Future objects are instances that will be created w
ithin the tested code.
Here is an example:
private void InitializeComponent() { this.okButton = new System.Windows.Forms.Button(); this.cancelButton = new System.Windows.Forms.Button(); this.ignoreButton = new System.Windows.Forms.Button(); }
We create 3 new Buttons in this method. Suppose we want to isolate the tested code from the Windows.Forms component and mock those buttons.
Here is the easy way:
//Mock all Buttons Mock allButtonsMock = MockManager.MockAll(typeof(Button));
We mock all the Button objects (both future and past). This will mock all our buttons but we won’t be able to set expectations on a specific instance. So if we setup an expecation, the expected method will be mocked for the first instance that calls that specific method.
Here is how to have better control on the instances.
// Mock future instance of Button (okButton) Mock okButtonMock = MockManager.Mock(typeof(Button)); // Mock next instance of Button (cancelButton) Mock cancelButtonMock = MockManager.Mock(typeof(Button)); // Mock future instance of Button (ignoreButton) Mock ignoreButtonMock = MockManager.Mock(typeof(Button));
Now we can control each button seperatly, notice how the SEQUENCE of the Button constructors mathces the ORDER of the mock assignments. This is how TypeMock works, each time MockManager.Mock is called the next constuctor is mocked and all expectations are tied to the specific instance.
Using this and the Firing Events techniques we can simulate different button sequences.
// Mock ok Button Mock okButtonMock = MockManager.Mock(typeof(Button)); MockEvent clickOk = okButtonMock.ExpectAddEvent("Click"); // Mock cancel Button Mock cancelButtonMock = MockManager.Mock(typeof(Button)); MockEvent clickCancel = cancelButtonMock.ExpectAddEvent("Click"); // Mock ignore Button Mock ignoreButtonMock = MockManager.Mock(typeof(Button)); MockEvent clickIgnore = ignoreButtonMock.ExpectAddEvent("Click"); // run our code RunCodeThatCallsInitializeComponents(); // Test Button Clicks clickCancel.Fire(this,EventArgs.Empty);
2 Comments to “How To Isolate Future Instances”
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

Mock okButtonMock = MockManager.Mock(typeof(MenuItem);
The code above, why use the type of “MenuItem” to mock button, that is a little bit confusing
Thanks for pointing this out.