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
Hi. I am trying to mock the firing of an event in 3.7.1 (enterprise license) and am getting the following error:

Test method UnitTest.ClassDerivedTest.TestOnResetEventIsCalled threw exception: TypeMock.TypeMockException:
*** No handlers are registered to the event.


This is happening in the case where my event is in the base class. I have created a small example.

using System;

namespace EventBug
{
public class ClassBase
{
public delegate void ResetHandler();
public event ResetHandler OnReset;
}

public class ClassDerived : ClassBase
{
public ClassDerived()
{
OnReset += new ResetHandler(MyOnReset);
}

public void MyOnReset()
{
Console.WriteLine("Handler is called");
}
}
}

My test is

// The following code was generated by Microsoft Visual Studio 2005.
// The test owner should check each test for validity.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Collections.Generic;
using EventBug;
using TypeMock;

namespace UnitTest
{
/// <summary>
///This is a test class for EventBugTypeMock.ClassDerived and is intended
///to contain all EventBugTypeMock.ClassDerived Unit Tests
///</summary>
[TestClass()]
public class ClassDerivedTest
{
[TestMethod()]
public void TestOnResetEventIsCalled()
{
Mock mockEvent = MockManager.MockAll(typeof(ClassBase)); // I have also tried ClassDerived here.
MockedEvent onReset = mockEvent.ExpectAddEvent("OnReset");
ClassDerived dc = new ClassDerived();
onReset.Fire();
MockManager.Verify();
}

}
}

I am not sure what is wrong. I have a test working where the event is not part of a base class.

Thanks for your help.
Sam
asked by darkelf1217 (1.3k points)

2 Answers

0 votes
Hi
The problem is that by default the constructor is mocked
so if you'll change the mock statement to

Mock mockEvent = MockManager.MockAll(typeof(ClassDerived), Constructor.NotMocked);


The test will run just fine 8)
answered by ohad (35.4k points)
0 votes
Thanks Ohad. That fixed the test for 1 of my 2 event handlers. The other one is exactly the same but comes from an abstract class. It works in my little test case (with an abstract class) but not in my real code. I'll have to look into it more.

Sam
answered by darkelf1217 (1.3k points)
...