chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I am new to Typemock and can not determine how to implement this test. If someone could please point me in the right direction it would be much appreciated.
The problem
I have three MenuItems created in my program that provide the Click event to different event handlers. I would like to fire each of these event handlers

The program
MenuItem addItem = new MenuItem();
addItem.Text = "&Add...";
addItem.Click += new EventHandler(addItem_Click);

MenuItem editItem = new MenuItem();
editItem.Text = "&Edit...";
editItem.Click += new EventHandler(editItem_Click);

MenuItem deleteItem = new MenuItem();
deleteItem.Text = "&Delete..."
deleteItem.Click += new EventHandler(deleteItem_Click);

The test code
// Create the mock of all MenuItems
Mock contextMenuItemsMock = MockManager.MockAll(typeof(MenuItem);

// Set the expectations
contextMenuItems.ExpectAddEvent("Click", 3);

My questions are:
1) How do I obtain the MockedEvent handle to each of the three events? Each handler is subscribed to a different instances of a MenuItem.

Depending on the answer to one above:
2) Should a method exist such as:
MockedHandle[] Mock.ExpectAddEventAlways(string name);

As this would mean the test would not fail if additional MenuItems were created in the program

Thanks for your time

asked by Paul (1.2k points)

2 Answers

0 votes
Hi Paul ,
Welcome to TypeMock :-)

This is actually an API bug in TypeMock, as this case should return multiple MockedEvent, we also don't have a ExpectAddEventAlways I will add this to our feature list.

Here is a workaround with Reflective Mocks. (See Mocking Events Documentation For Natural Mocks)

The Test Code
Mock addMenu = MockManager.Mock(typeof(MenuItem); 
MockedEvent clickAdd = addMenu.ExpectAddEvent("Click");

Mock editMenu = MockManager.Mock(typeof(MenuItem); 
editMenu .ExpectSetAlways("Text"); 
MockedEvent clickEdit = editMenu .ExpectAddEvent("Click");

Mock deleteMenu = MockManager.Mock(typeof(MenuItem); 
deleteMenu .ExpectSetAlways("Text"); 
MockedEvent clickDelete = deleteMenu .ExpectAddEvent("Click");

// to fire the edit menu...
clickEdit.Fire(this, EventArgs.Empty);

Thanks for reporting this
answered by scott (32k points)
0 votes
Hi Scott,

Thanks for the welcome, prompt response and great product. :)

From your example my understanding is that the sequence in which the mock objects are created matches the construction assignment within the code under test.

Your workaround has solved my problem. Thanks for the pointer
answered by Paul (1.2k points)