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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
TestedClass:
GBObject gbObject;

this.m_gbObject.MouseLeftButtonDown -=
new MouseButtonEventHandler(this.OnGBObjectMouseLeftButtonDown);
this.m_gbObject.MouseMove -= new MouseEventHandler(this.OnGBObjectMouseMove);
this.m_gbObject.MouseLeftButtonUp -=
new MouseButtonEventHandler(this.OnGBObjectMouseLeftButtonUp);

TestClass:
GBObject gbObjectMockObject = RecorderManager.CreateMockedObject<GBObject>();

using(RecordExpectations recorder = new RecordExpectations())
{
gbObjectMockObject.MouseLeftButtonDown -= null;
gbObjectMockObject.MouseMove -= null;
gbObjectMockObject.MouseLeftButtonUp -= null;
sizeHandleMockObject.HideHandle();
}

But it does not work?How can I do with "-= "?
asked by xuyingying (1.2k points)

6 Answers

0 votes
Hi xuyingying.

You would need to use the Reflective Mocks ExpectRemoveEvent() method - the documentation can be found in our developer's guide here: https://www.typemock.com/Docs/UserGuide/ ... ctive.html.

You can expect a test testing the MouseMove event unregisters in HideHandle() to look like this:
[Test, VerifyMocks]
public void CallingHideHandle_UnregistersMouseMoveEvent()
{
    // create mock object and define the expectation to removing the MouseMove event
    var mockGBObject = MockManager.Mock<GBObject>();
    mockGBObject.ExpectRemoveEvent("MouseMove");

    // create the object under test and call the method unregistering the event
    var gbObject = new GBObject();
    gbObject. HideHandle();
}


Please let me know if this helps.
Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
TestedClass
MockObject<GBObject> gbObjectMockObject = MockManager.MockObject<GBObject>();

gbObjectMockObject.ExpectRemoveEvent("MouseLeftButtonDown");
gbObjectMockObject.ExpectRemoveEvent("MouseMove");
gbObjectMockObject.ExpectRemoveEvent("MouseLeftButtonUp");
ObjectState gbObjectControlState = new ObjectState(objectControlMockObject);
gbObjectControlState.SetField("m_gbObject", gbObjectMockObject.Object);

when it run at
this.m_gbObject.MouseLeftButtonDown -=
new MouseButtonEventHandler(this.OnGBObjectMouseLeftButtonDown);
it will fail.I don't konw why.It says"System.NullReferenceException : ƒIƒuƒWƒFƒNƒgŽQ
answered by xuyingying (1.2k points)
0 votes
Hi

Sorry, we have a bug in the Mock.ExpectRemoveEvent :evil:
We'll let you know once it is fixed.
answered by ohad (35.4k points)
0 votes
Is this now fixed?
answered by JK (3.1k points)
0 votes
JK,

This bug was assigned with relatively low priority and was not fixed. Can you post here what you are trying to test? There are often better alternatives to testing interaction on event adders, so I may be able to help by pointing you in another direction, or elevate the bug's priority if there's no workaround.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
We have a workaround. We created a fake test class and implemented event add and remove methods. I think it works great :)
answered by JK (3.1k points)
...