使用起订量和TDD,从哪里开始? [英] Using Moq and TDD, where to start?

查看:172
本文介绍了使用起订量和TDD,从哪里开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器应用程序,我想知道,我应该开始,如果我要开始实施TDD和使用起订量。

I have a server application and I was wondering where I should start if I want to start implementing TDD and using Moq.

我可以阅读关于这个问题有什么好书,这是不是太网络为本?

What good books I could read on the subject, which aren't too "web-oriented"?

我对此事的问题,比如:

I have questions on the matter, like:

我应该嘲笑每个对象我想要测试,或只有那些我无法实现,如文本的作家?

Should I mock every object I want to test, or only those which I can't implement, like text writers?

我的服务器需要大量的安装程序,它实际上可以做任何我想要测试之前,我应该只是补习班该成 [TestInitialize] 功能?

My server needs a lot of setup before it can actually do anything I want to test, should I just cram that into a [TestInitialize] function?

我应该如何链我的测试中,如果我想测试更深入的功能?

How should I chain my tests, if I want to test deeper functionality?

推荐答案

我推荐两本书:的 测试驱动用例开发 ,由Kent Beck。这是对TDD的优秀图书,其中我特别喜欢,因为他走过的例子,这是获得一个意义的节奏和思维过程非常有用的。在另一方面,它是在嘲讽略显清淡。为此,我会读 单元测试的艺术 ,罗伊Osherove。正如标题所暗示的,它没有专注于TDD具体而言,而是就如何写出好的单元测试;他对嘲弄和存根一个良好的覆盖。

I recommend two books: Test Driven Development by Example, by Kent Beck. It's an excellent book on TDD, which I particularly enjoy because he walks through an example, which is very useful in getting a sense for the rhythm and thought process. On the other hand, it's a bit light on mocking. For that I would read The Art of Unit Testing, by Roy Osherove. As the title suggests, it's not focused on TDD specifically, but rather on how to write good unit tests; he has a good coverage on mocks and stubs.

至于你应该嘲笑什么,嘲讽的想法是让你隔离类/函数,你是从环境的其余部分进行测试,这样就可以测试其对你控制一个假的环境行为。在这个框架,你不应该嘲笑类,而是东西它取决于。

Regarding what you should mock, the idea of mocking is to allow you to isolate the class/function you are testing from the rest of the environment, so that you can test its behavior against a fake environment you control. In that frame, you should not be mocking the class, but rather things it depends upon.

一个简单的例子:如果你使用一个记录器,检测出类写道:到记录器将是非常痛苦的有一类,并且可能涉及的东西如检查是否记录器写在文本文件中。这不是很多层次的一个好主意 - 开始的事实,你的类不关心如何记录器专门做了这个工作。在这种情况下,你会用假的,嘲弄记录器更换Logger实例在你的类,然后你可以验证你的类呼吁在适当的时间记录器,无需担心记录器做什么,没错。

A trivial example: if you had a class using a Logger, testing that the class "writes" to the logger would be very painful, and could involve things like checking whether the logger has written in a text file. This is not a good idea on lots of levels - starting with the fact that your class doesn't care about how the logger does its job specifically. In that case you would replace the Logger instance in your class with a Fake, mocked Logger, and you can then verify that your class is calling the Logger at appropriate times, without worrying about what the logger does, exactly.

关于服务器初始化:单元测试通常是在内存中,与没有依赖环境,所以,如果你正在做TDD,你应该不必这样做。一般来说,在一个单元测试过多(任何?)初始化代码是一个不好的迹象。

Regarding server initialization: a unit test is typically in memory, with no dependencies to the environment, so if you are doing TDD, you should probably not have to do that. In general, too much (any?) initialization code in a unit test is a bad sign.

这表明,你正在寻找更多验收测试/ BDD风格的测试。我在电子书籍MSDN杂志与SpecFlow和华廷 行为驱动开发此最近的一篇文章;它解释了如何通过共同发展高层次的测试验证了该应用程序是做什么的用户希望(验收测试,在这里你可以运行实际的服务器和应用程序)在测试先行的方式发展,那它做它由具有小块代码,做什么开发计划(单元测试)。

This suggests that you are looking more for acceptance tests / BDD style tests. I recomment this recent article in MSDN Magazine on Behavior-Driven Development with SpecFlow and WatiN; it explains how you can develop in a test-first manner by developing together high-level tests which verify that the application is doing what the user wants (acceptance tests, where you would run your actual server and app), and that it's doing it by having small pieces of code that do what the developer intends (unit tests).

希望这可以帮助,快乐测试!

Hope this helps, and happy testing!

这篇关于使用起订量和TDD,从哪里开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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