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 within 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);

Bookmark at:
Add 'How To Isolate Future Instances' to Del.icio.us Add 'How To Isolate Future Instances' to digg Add 'How To Isolate Future Instances' to reddit Add 'How To Isolate Future Instances' to Feed Me Links! Add 'How To Isolate Future Instances' to Technorati Add 'How To Isolate Future Instances' to Yahoo My Web Add 'How To Isolate Future Instances' to Newsvine Add 'How To Isolate Future Instances' to FURL Add 'How To Isolate Future Instances' to blinklist Add 'How To Isolate Future Instances' to My-Tuts 

19 November 2006 | .NET Tests | Comments | Print This Post

2 Responses to “How To Isolate Future Instances”

  1. 1 Kun 20 November 2006 @ 10:34 pm

    Mock okButtonMock = MockManager.Mock(typeof(MenuItem);

    The code above, why use the type of “MenuItem” to mock button, that is a little bit confusing :-)

  2. 2 Eli Lopian 21 November 2006 @ 1:20 am

    :oops: My Mistake, fixed
    Thanks for pointing this out.

Leave a Reply

  1.  
  2.  
  3.  
Search Eli Lopian’s Blog (TypeMock)

Navigation

Recent Posts

Categories

Archives

Managment