为什么我需要一个嘲讽的框架,我的单元测试? [英] Why do I need a mocking framework for my unittests?

查看:141
本文介绍了为什么我需要一个嘲讽的框架,我的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近有相当周围的一切在.NET世界中不同的嘲讽框架的一些炒作。我仍然还没有完全掌握什么是如此之大,他们。它似乎并没有是很难写,我需要自己的嘲弄对象。特别是与Visual Studio的帮助下我很快就可以编写一个实现我想要模拟接口的类(它自动生成几乎所有的东西对我来说),然后写我需要为我的测试方法(S)的实现。完成!为什么经历的了解保存的code几行的唯一目的嘲弄框架的麻烦。或者是一个嘲弄框架不仅是节省了code线?

Recently there has been quite some hype around all the different mocking frameworks in the .NET world. I still haven't quite grasped what is so great about them. It doesn't seem to be to hard to write the mocking objects I need myself. Especially with the help of Visual Studio I quickly can write a class that implements the interface I want to mock (it auto-generates almost everything for me) and then write an implementation for the method(s) I need for my test. Done! Why going through the hassle of understanding a mocking framework for the sole purpose of saving a few lines of code. Or is a mocking framework not only about saving lines of code?

推荐答案

有一次,我终于得到了模仿对象的坑,我意识到,他们是必不可少的单元测试同样的原因,双盲试验组和对照组是必不可少的科学试验:他们隔离你实际上测试。

Once I finally got the hang of mock objects, I realized that they're essential for unit testing for the same reason that double blind testing or control groups are essential for scientific trials: they isolate what you're actually testing.

如果您正在测试它有相当多通过其它接口进行交互的一类,不仅节省了code线路上有嘲笑每个接口,而且还获得做事的能力或者像如果一个意想不到的方法被称为抛出异常,异常,如果这些方法被称为坏了。你可以得到显着的复杂与模拟框​​架,虽然我会很快承认有一个大的学习曲线,当你加快速度,他们会帮助你的单元测试更彻底而不臃肿。

If you're testing a class which has quite a bit of interaction via other interfaces, you not only save the lines of code on having to mock each and every interface, but you also gain the ability to do things like "throw an exception if an unexpected method is called" or "exception if these methods are called out of order". You can get remarkably sophisticated with mock frameworks, and though I'll quickly admit there's a large learning curve, when you get up to speed they'll help make your unit tests more thorough without being bloated.

这篇关于为什么我需要一个嘲讽的框架,我的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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