Moq新手测试设置 [英] Moq newbie test setup

查看:100
本文介绍了Moq新手测试设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要跟上Moq框架的步伐,我在表单上有一个按钮可以启动另一个表单.

I'm coming up to speed with the Moq framework and I have a button on a form that launches another form.

无论是否触发Click或DoubleClick事件,客户端都希望表单启动.

The client wants the form to launch whether the Click or DoubleClick event is fired off.

我对Moq框架的理解是,当您要模拟某些类型的动作时,它很有用:(为简便起见,我仅列出几种类型的动作)

My understanding of the Moq framework is that it is useful when you want to simulate certain types of actions: (For brevity, I'm only listing a few types of actions)

  1. 连接到数据库
  2. 写入文件
  3. 确定网络连接

但是我不确定Moq如何与Windows控件和事件进行交互.

But I'm not 100% certain on how Moq can interact with Windows controls and events.

任何人都可以通过一个简单的例子使我朝正确的方向前进吗?

推荐答案

在这种情况下,我不会使用Moq或单元测试.

I wouldn't use Moq or unit testing for that scenario.

您具有在可视控件下启动其他表单的代码.从本质上讲,这是无法单独测试的.如果需要,您可以使用其他工具来自动化集成测试.

You have code under a visual control that launches another form. That's inherently not testable in isolation. You could automate integration tests with other tools if you wanted to.

Moq用于启用单元测试.单元测试隔离地测试功能(通常是逻辑).很难应用于用户界面.

Moq is designed for enabling unit testing. Unit testing tests functionality (often logic) in isolation. It's difficult to apply to user interfaces.

通常,您要做的是使用依赖注入可以打破依赖关系,然后使用诸如Moq之类的隔离框架为这些接口创建虚假的实现,以便您可以独立测试子系统.

Normally what you'd do is go with one of the Model-View-Controller variants, separate your presentation layer from your business logic, use interfaces and Dependency Injection to break dependencies, then use an isolation framework such as Moq to create fake implementations of those interfaces so that you could test subsystems independently.

如果这听起来很复杂,那就是.编写经过测试的大型应用程序也很重要.

If this sounds complicated, it is. It's also critical to writing large, tested applications.

我建议先阅读 .NET中带有示例的单元测试的技巧由Roy Osherove撰写.这涵盖了很多基础知识.

I'd recommend starting by reading the book The Art of Unit Testing with Examples in .NET by Roy Osherove. That covers a lot of the basics.

这篇关于Moq新手测试设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆