验证执行的事件处理程序代码

发布于 2025-01-07 11:32:31 字数 862 浏览 1 评论 0原文

我的代码与此非常相似,但无法弄清楚如何测试事件处理程序是否发生。

public class MyClass : MyAbstractClass
{ 
  IFileSystem FileSystem;

  public MyClass(IFileSystem myFileSys)
  {
    FileSystem = myFileSys;
    FileSystem.EventHit += new EventHandler(FileSystem_EventHit);
  }

  public void FileSystem_EventHit(object sender, EventArgs e)
  {
    //Testing base.OnOutput is not possible which I wont go into
    base.OnOutput("EventHit");
  }
}

测试代码在这里:

    [Test]
    public void DoSomething_WhenCalled_EventFired()
    {
         var mock = new Moq.Mock<IFileSystem>();

         MyClass plugin = new MyClass (mock.Object);

         mock.Object.DoSomething();

         mock.Raise(x => x.EventHit += null, new EventArgs());

        //Verify/Assert that MyClass handled and did something in the event handler

    }

I have code very similar to this but cannot work out how I test whether an event handler occured.

public class MyClass : MyAbstractClass
{ 
  IFileSystem FileSystem;

  public MyClass(IFileSystem myFileSys)
  {
    FileSystem = myFileSys;
    FileSystem.EventHit += new EventHandler(FileSystem_EventHit);
  }

  public void FileSystem_EventHit(object sender, EventArgs e)
  {
    //Testing base.OnOutput is not possible which I wont go into
    base.OnOutput("EventHit");
  }
}

Testing code is here:

    [Test]
    public void DoSomething_WhenCalled_EventFired()
    {
         var mock = new Moq.Mock<IFileSystem>();

         MyClass plugin = new MyClass (mock.Object);

         mock.Object.DoSomething();

         mock.Raise(x => x.EventHit += null, new EventArgs());

        //Verify/Assert that MyClass handled and did something in the event handler

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清君侧 2025-01-14 11:32:31

我能想到的最简单的方法是在测试方法中添加您自己的处理程序,我认为这应该足够了?

[Test]
    public void DoSomething_WhenCalled_EventFired()
    {
         var mock = new Moq.Mock<IFileSystem>();
         bool isHit = false;

         mock.EventHit += (s, e) =>
         {
            isHit = true;
         };

         MyClass plugin = new MyClass (mock.Object);

         mock.Object.DoSomething();

         mock.Raise(x => x.EventHit += null, new EventArgs());

         Assert.IsTrue(isHit);

    }

The simplest way I can think of is to just add your own handler in the test method, which should suffice I would think?

[Test]
    public void DoSomething_WhenCalled_EventFired()
    {
         var mock = new Moq.Mock<IFileSystem>();
         bool isHit = false;

         mock.EventHit += (s, e) =>
         {
            isHit = true;
         };

         MyClass plugin = new MyClass (mock.Object);

         mock.Object.DoSomething();

         mock.Raise(x => x.EventHit += null, new EventArgs());

         Assert.IsTrue(isHit);

    }
居里长安 2025-01-14 11:32:31

由于验证事件处理程序中的某些内容意味着尝试测试遗留代码,因此我选择的选项是测试事件是否从具体类型而不是模拟中触发。

[Test]
public void DoSomething_WhenCalled_EventFired()
{

  FileSystem fs = new FileSystem(mock.Object, timerMock.Object);

  bool WasItHit = false;
  fs.EventHit += delegate { WasItHit = true; };

  fs.DoSomething(); //This should call the event

  Assert.IsTrue(WasItHit);
}

As verifying something in the event handler would mean trying to test legacy code the option I went with was to test that the event fired from within the concrete type and not a mock.

[Test]
public void DoSomething_WhenCalled_EventFired()
{

  FileSystem fs = new FileSystem(mock.Object, timerMock.Object);

  bool WasItHit = false;
  fs.EventHit += delegate { WasItHit = true; };

  fs.DoSomething(); //This should call the event

  Assert.IsTrue(WasItHit);
}
余生再见 2025-01-14 11:32:31

您需要注入事件处理程序调用结果所调用的任何内容的模拟并验证它。您的评论说您无法测试 base base.OnOutput,但在我看来这正是您需要做的。

You need to inject a mock of whatever gets called as a result of the event handler invocation and verify it. Your comment says you can't test base base.OnOutput, but it seems to me that is exactly what you need to do.

笑梦风尘 2025-01-14 11:32:31

基本上测试调用方法的事实不是有效的测试用例,您应该测试方法背后的逻辑/行为。显然,对于给定的事件处理程序,没有什么可测试的,这就是为什么任务看起来并不简单。

尝试用几句话来表述您要测试什么,哪个测试用例。例如

MyClass 将状态切换到 State==Hit 同时
FileSystem.EventHit 事件。

Basically testing of a fact that method was called is not a valid test case, you should test a logic/behaviour behind a method. Obviously with a given event handler there is nothing to test, this is why a task looks not trivial.

Try out formulate in few words what are you trying to test, which test case. For instance

MyClass switches a state into the State==Hit whilst
FileSystem.EventHit event.

本宫微胖 2025-01-14 11:32:31

为此,您可能需要在 MyClass 中使用一个标志来指示该事件发生。我知道,这只是为了运行测试,但有时有这样的东西是很好的。

To do that you probably need a flag in MyClass indicating that event occured. I know, this will be just for purpose of running a test but sometimes is good to have something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文