Wednesday, 22 February 2006

Unit Testing Events with Anonymous Delegates in .NET 2.0

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:

        [TestMethod()]
        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;
            UniSA.UniSAnet.SmartMark.DataAccess.TestComponent.UpdateTestTimes(test);
 
            Assert.AreEqual(expected, actual);
 
        }

No comments: