问题描述
我一直在专门使用 ruby 大约一个月,我真的很喜欢它.但是,我在使用甚至学习 TDD 时遇到了难以置信的困难.我的大脑不能那样运作...
I have been using ruby exclusively for about a month and I really love it. However, I am having an incredibly hard time using, or even learning TDD. My brain just doesn't function that way...
我真的,真的很想学习 TDD,但说实话,我有点困惑.我在谷歌搜索时发现的所有文章大多是针对 Rails 的,这对我来说并不有趣,因为我想学习如何对任何 ruby 应用程序进行有效的测试,从简单的单文件脚本到复杂的 gem,而不是为网络应用程序.此外,要开始使用的框架太多,教程却太少.
I really, really want to learn TDD but to be honest I am a bit confused. All the articles that I find when Googling around are mostly specific to Rails, which is not interesting to me because I want to learn how to do efficient testing for any ruby application, from the simple one-file script to the complicated gem, not for Web apps. Also, there are so many frameworks and so few tutorials to get started.
有人能给我关于如何学习 TDD 的任何建议,以便我至少可以开始认为自己是一个有抱负的 rubyist 吗?
Could anybody give me any advice on how to learn TDD so that I can at least start to consider myself an aspiring rubyist?
推荐答案
学习 TDD 的最佳方式就是亲自动手.我建议您使用 TDD 构建一个新项目.这意味着除非测试失败,否则不要编写任何非测试代码.
The best way to learn TDD is by just doing it. I suggest you build a new project using TDD. This means don't write any non-testing code unless you have a failing test.
它会让你考虑编写测试:我想编写这段代码,我如何为它编写测试以便我可以编写它.
It will make you think about writing tests: I want to write this code, how do I write a test for it so I can write it.
它将向您展示测试的分层性质.而不是想要一个必需且不能包含数字的名称.您将首先测试设置和读取名称,测试要求名称,测试它不应该包含数字,而不是考虑它是否有更多约束并测试它们.
It will show you the layered nature of testing. Instead of want a name that is required and can't contain numbers. you'll first test setting and reading a name, test requiring the name, test it shouldn't contain numbers, than think if it has more constraints and test those.
记住:
- 在编写代码之前先编写测试
- 确保测试失败!重要的是要知道您正在测试的逻辑是正确的
- 在编写下一个测试之前,请确保所有测试都成功
- 你可以随时清理你的代码,如果测试继续工作,你没有改变设计
这篇关于如何用 Ruby 学习 TDD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!