如何在VB.net单元测试期间抑制弹出消息框
想知道你是如何做到这一点的?
因为每次弹出消息框都要点击“确定”,这很烦人。
Was wondering how you do this?
As it's very annoying to click "ok" everytime a message box pops up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
理想情况下,消息框被设置为依赖项,以便在单元测试期间可以对其进行模拟/存根/伪造,如下所示:
当然,您需要在 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:
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.
理想情况下,您的测试将测试您的业务逻辑。我通常将 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.