如何在VB.net单元测试期间抑制弹出消息框

发布于 2024-12-08 01:41:52 字数 53 浏览 2 评论 0原文

想知道你是如何做到这一点的?

因为每次弹出消息框都要点击“确定”,这很烦人。

Was wondering how you do this?

As it's very annoying to click "ok" everytime a message box pops up.

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

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

发布评论

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

评论(2

请帮我爱他 2024-12-15 01:41:52

理想情况下,消息框被设置为依赖项,以便在单元测试期间可以对其进行模拟/存根/伪造,如下所示:

public interface IMessageBox
{
  Show(...);
}

当然,您需要在 MessageBox 包装类中实现该接口。有关 DI 的更多信息:http://en.wikipedia.org/wiki/Dependency_injection

如果不可能现在要以这种方式重新设计您的应用程序,您可以尝试 这种方法

Ideally, the message box is set up as a dependency so that it could be mocked/stubbed/faked during unit testing, something like this:

public interface IMessageBox
{
  Show(...);
}

Of course, you would need to implement the interface in your MessageBox wrapper class. More about DI: http://en.wikipedia.org/wiki/Dependency_injection

If it's not possible to re-engineer your application this way now, you can try this approach.

是伱的 2024-12-15 01:41:52

理想情况下,您的测试将测试您的业务逻辑。我通常将 MessageBox 视为 UI 组件而不是 UI。这需要修改程序流程,以便错误/异常/验证的发生独立于用户通知。 IE 依赖 IDataErrorInfo 而不是属性集异常。

如果您无法避免它并且不想走包装消息框并使用 DI 模拟的路线,我使用了一种设置 Public Shared Property RunSilent As Boolean = False singleton 变量指示应用程序应该以静默方式运行,然后在调用消息框之前检查该属性,以便仅在我们不以静默方式运行时才显示该框。然后在我的单元测试设置中,将静音标志设置为 true。不理想,但有效。

Ideally, your tests would be testing your business logic. I typically view the MessageBox as a UI component rather than UI. This requires modifying the program flow so that errors/exceptions/validation happens independantly of the user notifications. IE relying on IDataErrorInfo instead of exception on property set.

In cases where you can't avoid it and don't want to go the route of wrapping the messagebox and using mocks with DI, I've used a technique of setting a Public Shared Property RunSilent As Boolean = False singleton variable in my applications to indicate that the applciation is supposed to run silent and then check that property before calling to the messagebox to only show the box if we aren't running silent. And then in my unit test setup, set the silent flag to true. Not ideal, but effective.

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