Unit Testing Events with Anonymous Delegates in .NET 2.0

Written on February 22, 2006

How do you unit test your event code? You use Anonymous Delegates

Here’s a sample unit test that I’ve created that makes use of this technique:


    public void UpdateTestTimesTest()


        object expected = UniSA.UniSAnet.SmartMark.DataAccess.DataItemChangedEventArgs.ChangeType.Update;

        object actual = null;

        UniSA.UniSAnet.SmartMark.DataAccess.TestComponent.DataItemChanged += delegate(object sender, UniSA.UniSAnet.SmartMark.DataAccess.DataItemChangedEventArgs e)


            actual = e.Change;


        Test test = UniSA.UniSAnet.SmartMark.DataAccess.TestComponent.GetTest(UnitTestingTestId);

        test.MinutesAllowed += 1;


        Assert.AreEqual(expected, actual);