我发现Phil Haack撰写了关于如何使用匿名委托测试事件的文章
这是代码,直接从他的博客中删除那些懒得点击的人:
[Test] public void SettingValueRaisesEvent() { bool eventRaised = false; Parameter param = new Parameter("num", "int", "1"); param.ValueChanged += delegate(object sender, ValueChangedEventArgs e) { Assert.AreEqual("42", e.NewValue); Assert.AreEqual("1", e.OldValue); Assert.AreEqual("num", e.ParameterName); eventRaised = true; }; param.Value = "42"; //should fire event. Assert.IsTrue(eventRaised, "Event was not raised"); }