SourceForge has been redesigned. Learn more.
Close

#25 Event unsubscribe in an event handler does not work

closed-fixed
nmock2 (11)
5
2008-02-25
2008-02-21
Tom Geelen
No

When you fire an event on a mock object and the event handler removes (or adds, but that's less realistic) its handler for that event, an InvalidOperationException ("Collection was modified") is thrown. With normal C# event handling, this works.

Example:

delegate void BlahEventHandler();
public interface IInterfaceToMock
{
event BlahEventHandler Blah;
}

public class MyClass()
{
// ...
public MyClass(IInterfaceToMock itm)
{
// ...
itm.Blah += OnBlah;
}
private void OnBlah()
{
DoSomething();
itm.Blah -= OnBlah;
}
}

[TestFixture]
public class MyClassTest
{
[Test]
public void testEvent()
{
// ...
IInterfaceToMock mockInterfaceToMock = mocks.NewMock<IInterfaceToMock>();
Expect.Once.On(mockInterfaceToMock).EventRemove("Blah", Is.Anything);
Fire.Event("Blah").On(mockInterfaceToMock).With(); //This call fails...
}
}

A possible solution is attached (doesn't include tests, but doesn't break any existing test).

Discussion

  • Richard A. Holden III

    Logged In: YES
    user_id=591527
    Originator: NO

    Fixed in CVS, thanks for the good bug report and patch.

     
  • Richard A. Holden III

    • assigned_to: nobody --> aciddeath
    • status: open --> closed-fixed
     
  • Thomas Weingartner

    Logged In: YES
    user_id=1140914
    Originator: NO

    It's not a problem with NMock2 either (see http://nmock2.sourceforge.net\)

    Cheers
    Thomas

     

Log in to post a comment.