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

I have the following code which i am using to try and mock an event:


    public class ClassWithEvent
        public event EventHandler<EventArgs> MyEvent;

    [TestMethod, Isolated]
    public void MockMyEvent()
        var myClass = Isolate.Fake.Instance<ClassWithEvent>();

        bool hasBeenCalled = false;

        myClass.MyEvent += (sender, args) => { hasBeenCalled = true; };

        Isolate.Invoke.Event(() => myClass.MyEvent += null, EventArgs.Empty);


When run, the test fails due to a TypeMockException being thrown:

TypeMock.TypeMockException : 
*** Event expects arguments of types {Object, EventArgs} use Isolate.Invoke.Event(()=>fake.Event+=null,arg1,arg2...);
Could not invoke with arguments of types {EventArgs}
I've looked over and over this and must be missing something.
Any help would be great :)


asked by KBrown (1.7k points)

1 Answer

+1 vote
Best answer

Hi KBrown,



Please add another EventArgs.Empty :

Isolate.Invoke.Event(() => myClass.MyEvent += nullEventArgs.Empty, EventArgs.Empty);

It should solve your issue.

Let us know if it helps.

answered by Bar (3.3k points)
selected by KBrown
Thanks for your answer. However, neither of the suggestions above work for me.

This should fix your issue:

Isolate.Invoke.Event(() => myClass.MyEvent += nullEventArgs.Empty, EventArgs.Empty);


Isolate.Invoke.Event(() => myClass.MyEvent += null, thisEventArgs.Empty);


This is because arg 2 is the sender and arg 3 is the EventArgs - this is sent to the event handler.

You can also use the default (since versions 8)

Isolate.Invoke.Event(() => myClass.MyEvent += null);


This will default to passing this and EventArgs.Empty