使用起订量和 TDD,从哪里开始?

发布于 2024-11-01 23:57:45 字数 275 浏览 1 评论 0原文

我有一个服务器应用程序,我想知道如果我想开始实施 TDD 并使用 Moq,我应该从哪里开始。

我可以阅读哪些关于该主题的好书,但又不太“面向网络”?

我对此事有疑问,例如:

我应该模拟我想要测试的每个对象,还是只模拟那些我无法实现的对象,例如文本编写器?

我的服务器需要进行大量设置才能真正执行我想要测试的任何操作,我是否应该将其塞入 [TestInitialize] 函数中?

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

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?

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?

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

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

发布评论

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

评论(4

空城仅有旧梦在 2024-11-08 23:57:45

您不会模拟要测试的对象。如果你这样做,你正在测试模拟,而不是你的对象!您需要模拟您正在测试的对象的依赖关系

You don't mock the objects you want to test. If you do that, you're testing the mock, not your object! You need to mock the dependencies of the objects you're testing.

烦人精 2024-11-08 23:57:45

我推荐两本书:示例测试驱动开发 ,作者:肯特·贝克。这是一本关于 TDD 的优秀书籍,我特别喜欢,因为他详细介绍了一个示例,这对于了解节奏和思维过程非常有用。另一方面,嘲笑有点轻。为此,我会阅读 Roy Osherove 的单元测试的艺术。正如标题所示,它并不是专门关注 TDD,而是关注如何编写良好的单元测试;他对模拟和存根有很好的报道。

关于您应该模拟的内容,模拟的想法是允许您将正在测试的类/函数与环境的其余部分隔离,以便您可以针对您控制的虚假环境测试其行为。在该框架中,您不应该嘲笑该类,而应该嘲笑它所依赖的事物。

一个简单的例子:如果您有一个使用记录器的类,测试该类“写入”记录器将非常痛苦,并且可能涉及检查记录器是否已写入文本文件之类的事情。这在很多层面上都不是一个好主意 - 首先,您的类并不关心记录器具体如何完成其​​工作。在这种情况下,您可以将类中的 Logger 实例替换为假的、模拟的 Logger,然后您可以验证您的类是否在适当的时间调用 Logger,而不必担心记录器到底做了什么。

关于服务器初始化:单元测试通常在内存中进行,不依赖于环境,因此如果您正在进行 TDD,则可能不必这样做。一般来说,单元测试中太多(任何?)初始化代码是一个坏兆头。

这表明您更多地寻找验收测试/BDD 风格的测试。我推荐 MSDN 杂志上最近关于使用 SpecFlow 和 WatiN 进行行为驱动开发;它解释了如何通过一起开发高级测试来以测试优先的方式进行开发,这些测试验证应用程序是否正在执行用户想要的操作(验收测试,您将在其中运行实际的服务器和应用程序),并且它正在执行此操作通过使用小段代码来完成开发人员的意图(单元测试)。

希望这有帮助,祝测试愉快!

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.

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.

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.

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!

兰花执着 2024-11-08 23:57:45

我最喜欢的 TDD 书籍之一是测试驱动开发示例(Kent Beck) 。我也非常喜欢他所做的4部分屏幕截图

第 1 集: 入门测试(28 分钟)

在本集中,我们对示例应用程序的第一个功能进行第一次测试,并将其分割以提供更频繁的反馈。

第 2 集:独立测试(23 分钟)

在本集中,我们确保测试不会相互影响。一旦测试被隔离,我们就会实施一些新的操作。

第 3 集: 大专题(25 分钟)

在本集中,我们将选取一个大专题并将其分割以提供更频繁的反馈。最后,我们清理代码以消除重复并使代码更易于阅读。

第 4 集: 完成(20 分钟)

在本集中,我们完成了示例应用程序的功能并准备供其他人使用。开发早期推迟的设计决策现在更加清晰。该系列以所有剧集的教训总结结束。

One of my favorite books on TDD is Test Driven Development By Example (Kent Beck). I also really liked a 4-part screen cast he did.

Episode 1: Starter Test (28 minutes)

In this episode we take the first test for the first feature our sample application and slice it up to provide more-frequent feedback.

Episode 2: Isolated Tests (23 minutes)

In this episode we ensure that tests don’t affect each other. Once the tests are isolated we implement several new operations.

Episode 3: Big Feature (25 minutes)

In this episode we take a large feature and slice it up to provide more-frequent feedback. At the end we clean the code to remove duplication and make the code easier to read.

Episode 4: Finishing (20 minutes)

In this episode we finish the functionality of the sample application and prepare it for use by others. Design decisions that were deferred earlier in development are now clearer. The series closes with a summary of lessons from all of the episodes.

我是有多爱你 2024-11-08 23:57:45

如果您希望遵循 TDD 模式,您的代码应该随着测试的开发而不断发展。您将追求单一职责,并且如上所述模拟/存根您正在测试的类所具有的任何依赖项。通过这种方式,您可以在任何依赖项上设置虚拟数据和预期行为,而不必再担心它们。

这里有一个简单的介绍:http://www.agiledata.org/essays/tdd.html 不幸的是,我没有任何可以根据个人经验推荐的具体书名。

阅读本文也可能对您入门有所帮助: http://stephenwalther.com/blog/archive/2008/06/12/tdd-introduction-to-moq.aspx

Your code should evolve through the development of your tests, if you wish to the follow the TDD pattern. You'd be going for single-reponsibility and as mentioned mock/stub any dependencies the class you're testing has. This way you can setup dummy data and expected behaviours on any dependencies and not worry about them any further.

This has a brief introduction: http://www.agiledata.org/essays/tdd.html unfortunately I don't have any specific book titles I can recommend from personal experience.

Reading this may also be useful to get you started: http://stephenwalther.com/blog/archive/2008/06/12/tdd-introduction-to-moq.aspx

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