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 have to mock an event object and fire it when Mocked method is called. But I've known from another topic that it cannot mock object in the event handler. Is there any alternative way to do that? Kindly let me know. :)
asked by albusr (800 points)

8 Answers

0 votes
You can fire the event with in a dynamic return delegate.
Here is an example:
MockedEvent handle; // handle for event

[Test]
public void FireEventWithinAMockedMethof()
{
   // mock event and save handle
   Mock eventMock = MockManager.Mock(typeof(MyEvent));
   handle = eventMock.ExpectAddEvent("TheEvent");

   // mock a class and use a Dynamic Return Value
   Mock anotherMock = MockManager.Mock(typeof(OtherType));
   anotherMock.ExpectAndReturn("DoSomething", new DynamicReturnValue(FireIt));

    // continue test -> when DoSomething is called TheEvent will be fired
}
// the delegate called when DoSomething is called
public object FireIt(object[] parameters, object that)
{
   handle.Fire(this, null);
   return null;
}
answered by scott (32k points)
0 votes
Thank you very much. :D
answered by albusr (800 points)
0 votes
Sorry again. I try to mock the event argument object and fire it like the code below ... but it still not mock. Is there somthing wrong with my code?



MyEvent eventArg;

public void FireEventWithinAMockedMethof()
{
// mock event and save handle
MockObject eventMock = MockManager.MockObject(typeof(MyEvent));
eventMock.ExpectGetAlways("Properties","Value");
eventArg = (MyEvent)eventMock.object;
handle = eventMock.ExpectAddEvent("TheEvent");

// mock a class and use a Dynamic Return Value
Mock anotherMock = MockManager.Mock(typeof(OtherType));
anotherMock.ExpectAndReturn("DoSomething", new DynamicReturnValue(FireIt));

// continue test -> when DoSomething is called TheEvent will be fired
}
// the delegate called when DoSomething is called
public object FireIt(object[] parameters, object that)
{
handle.Fire(this, eventArg);
return true;
}
answered by albusr (800 points)
0 votes
The event is fired but "Properties" is not "Value" as expect.
answered by albusr (800 points)
0 votes
The TypeMock Tracer can help you here.
You will see the expecations and when they are called.
answered by scott (32k points)
0 votes
I've realised that an object cannot be mocked under the method defined on DynamicReturnValue like in the case of MockedMethodCalled event handler, right? I used the tracer to trace ... and it's still not mocked.
If you have some example that work please show me.

Thank you,
answered by albusr (800 points)
0 votes
Hi,
You are correct. Code called from within a DynamicReturnValue method is not mocked. We initially did this to stop inifite recursive calls. Although your scenario leads me to belive that we should rethink this. I will add it to our future features.

I think that a reasanable workaround in this case is to set the Properties value before sending the EventArg to Fire(). Although this won't cover all cases where mocking is required, it should do in your case.

// the delegate called when DoSomething is called
public object FireIt(object[] parameters, object that)
{
  MyEvent ev = new myEvent;
  ev.Properties = "Value"
  handle.Fire(this, ev);
  return true;
}
answered by scott (32k points)
0 votes
Thank you very much for your suggestion. By the way the EventArg property I want to fire cannot be assigned to, it's read only. I'll try another way. :)
answered by albusr (800 points)
...