如何开始使用 Ruby on Rails 进行 TDD?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1386562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get started on TDD with Ruby on Rails?
提问by marcgg
I am familiar with the concepts (took testing classes in college), but I am not sure how to really use them yet since I never worked on a "real" TDD project.
我熟悉这些概念(在大学上过测试课),但我不确定如何真正使用它们,因为我从未参与过“真正的”TDD 项目。
I am about to start the development of a project using Ruby on Rails (most likely using 2.3). This application will be used to manage data, users and some files. It won't be too complicated at first but might scale a lot in the next 6 months so I feel this is the right time to get more into TDD.
我即将开始使用 Ruby on Rails(很可能使用 2.3)开发一个项目。此应用程序将用于管理数据、用户和一些文件。一开始不会太复杂,但在接下来的 6 个月内可能会扩展很多,所以我觉得现在是深入研究 TDD 的合适时机。
I've got a basic idea on how to do it, but I still need some pointers and advices:
我对如何做有一个基本的想法,但我仍然需要一些指示和建议:
What Ruby on Rails TDD 101 article should I read?
What do I need to test?
What gem/plugin should I use?
Should I use rspec? Something else?
Once I've got all my testing classes, how do I go and deploy them? (e.g.: Continual Integration)
How time consuming TDD really is?
Do I need to read a book about this or can I get everything just by playing around with it and reading online tutorials? If I need to read a book, what book?
我应该阅读什么 Ruby on Rails TDD 101 文章?
我需要测试什么?
我应该使用什么 gem/插件?
我应该使用rspec吗?还有什么?
一旦我获得了所有的测试类,我该如何去部署它们?(例如:持续集成)
TDD 到底有多耗时?
我是否需要阅读有关这方面的书,或者我可以通过玩弄它并阅读在线教程来获得所有信息?如果我需要读一本书,读什么书?
I like learning with examples so could someone tell me how I would go and take a TDD approach to solve this issue:
我喜欢通过示例学习,所以有人可以告诉我我将如何采用 TDD 方法来解决这个问题:
I have Companies. I have Contacts. A contact can be linked to 1 company. A company can have multiple contacts. I want to create ways to create contacts, companies and link contacts to companies.
我有公司。我有联系人。一个联系人可以链接到 1 个公司。一个公司可以有多个联系人。我想创建创建联系人、公司和将联系人链接到公司的方法。
You don't have to use this example in your answer but it would help :)
您不必在答案中使用此示例,但它会有所帮助:)
回答by ez.
What Ruby on Rails TDD 101 article should I read?
我应该阅读什么 Ruby on Rails TDD 101 文章?
I will start with a guide to testing rails applications.
我将从测试 Rails 应用程序的指南开始。
Also Railscasthas some excellent screencasts about how to use different testing tools.
此外Railscast在如何使用不同的测试工具,一些优秀的截屏。
What do I need to test?
我需要测试什么?
I will start with models, since they are easy to test. The simple rule is that you need to cover every if statement in your test.
我将从模型开始,因为它们很容易测试。简单的规则是您需要覆盖测试中的每个 if 语句。
You should test the purpose of the method (to make sure it is functioning as expected) as well as all edge cases.
您应该测试该方法的用途(以确保它按预期运行)以及所有边缘情况。
Also make sure you don't end up over testing.
还要确保您最终不会过度测试。
What gem/plugin should I use? Should I use rspec? Something else?
我应该使用什么 gem/插件?我应该使用 rspec 吗?还有什么?
When you start, just use Test Unit. You can use rspecor cucumberafter you get familiar with the basics.
开始时,只需使用Test Unit. 您可以使用rspec或cucumber在您熟悉基础知识之后。
Autotestis a nice tool to have if you want to be truly test driven. But it is a 'nice have' not required.
Autotest如果您想真正进行测试驱动,这是一个很好的工具。但这是一个不需要的'好东西'。
Once I've got all my testing classes how do I go and deploy them?
一旦我完成了所有的测试课程,我该如何去部署它们?
Not sure about the question. You don't usually deploy the tests. Once you have all your testing classes simple type 'rake test' to run all your tests.
不确定这个问题。您通常不会部署测试。一旦你有了所有的测试类,简单地输入“rake test”来运行你的所有测试。
How time consuming TDD really is?
TDD 到底有多耗时?
It saves time really. If you like labyrinth puzzle, you know it is almost always easier to solve it if you go from finish to start. Same with TDD. Without Test Driven you are consistently thinking 'what should i do next'. With Test Driven, the test will tell you what to do next (it breaks if the logic is not there so you just need to fix the broken part). Also you have less bugs which will save you a lot of time in the long run.
它真的很节省时间。如果你喜欢迷宫拼图,你就会知道如果你从头到尾解决它几乎总是更容易。与 TDD 相同。如果没有测试驱动,你会一直在思考“我接下来应该做什么”。使用测试驱动,测试会告诉你接下来要做什么(如果逻辑不存在,它就会中断,所以你只需要修复损坏的部分)。此外,您的错误更少,从长远来看,这将为您节省大量时间。
Do I need to read a book about this or can I get everything just by playing around with it and reading online tutorials? If I need to read a book, what book?
我是否需要阅读有关这方面的书,或者我可以通过玩弄它并阅读在线教程来获得所有信息?如果我需要读一本书,读什么书?
You do not need a book. The most efficient way of learning anything is: just do it. Go back to the book or online resources once you encounter a question or problem. This is agile too.
你不需要一本书。学习任何东西最有效的方法是:就去做吧。一旦遇到问题或问题,请返回本书或在线资源。这也很敏捷。
In your example, the things that need testing are: A contact can be linked to 1 company, A company can have multiple contacts, create ways to create contacts, and link contacts to companies.
在您的示例中,需要测试的内容是:一个联系人可以链接到 1 个公司,一个公司可以有多个联系人,创建创建联系人的方法,并将联系人链接到公司。
class CompanyTest <Test::Unit
def test_relationship # test associations/relationships
c = companies(:some_company)
assert_equal [a list of contacts], c.contacts # make sure a company can have multiple contacts
end
end
class ContactTest<Test::Unit
def test_relationships
c = contact(:some_contact)
assert_equal some_company, c.company # make sure the contact link to 1 company
end
def test_create/add
# test create contacts, here you need to make sure the contact is created correctly, and linked to company correctly
end
end
回答by Wolfram Arnold
I've produced a 6-episode video series which was taught as a public class in San Francisco in the summer of 2010. The material covers testing and developer efficiency in Rails 2.3 using RSpec 1.3. Slightly dated, but the main concepts apply to Rails 3 with Rspec 2.x
我制作了一个 6 集的视频系列,于 2010 年夏天在旧金山作为公共课程教授。该材料涵盖了使用 RSpec 1.3 在 Rails 2.3 中的测试和开发人员效率。有点过时,但主要概念适用于带有 Rspec 2.x 的 Rails 3
http://www.rubyfocus.biz/class_video/2010/07/19/rails_tdd_class_1.html
http://www.rubyfocus.biz/class_video/2010/07/19/rails_tdd_class_1.html
回答by sivabudh
I recommend this book: Ruby on Rails Tutorial. I'm almost done with it. The book uses TDD the wholebook. Give it a try!
我推荐这本书:Ruby on Rails 教程。我快完成了。书中使用TDD的整本书。试一试!
回答by JRL
I recommend this book: Agile Web Development with Rails
回答by Andy Gaskell
TDD is all about writing tests first. This basically forces you to write your own client before you write your application code. The cycle is generally write a test for an API that doesn't exist, run the test expecting it to fail, go write your API code, run your test again and make sure it passes. Then write your next test... and so on.
TDD 就是首先编写测试。这基本上迫使您在编写应用程序代码之前编写自己的客户端。这个循环通常是为一个不存在的 API 编写测试,运行测试期望它失败,去编写你的 API 代码,再次运行你的测试并确保它通过。然后编写您的下一个测试……等等。
You might also be interested in this Rails guide.
您可能还对这个Rails 指南感兴趣。
回答by Mike
I use :
我用 :
- Shouldaand rspec for testing
- Mocha for mocking
- Factory_girlfor factories
- parallel_specsfor faster testing
- metric_fufor code analysis
- 用于测试的shoulda和 rspec
- 摩卡的嘲讽
- Factory_girl为工厂
- parallel_specs用于更快的测试
- metric_fu用于代码分析
回答by Patrick Robertson
What gem/plugin should I use?
我应该使用什么 gem/插件?
I've always enjoyed shoulda.
我一直很喜欢shoulda。
How time consuming TDD really is?
TDD 到底有多耗时?
The reason I've always favored TDD development is that it focuses how I will implement a specific piece of code. I have an anecdotal feeling that whenever I adhere more strongly to TDD principles I spend less time reworking later. The amount of time spent is all in how well you write unit tests though. If the unit tests don't capture the expected behavior, all the time spent on them is wasted.
我一直喜欢 TDD 开发的原因是它关注我将如何实现一段特定的代码。我有一种轶事感觉,每当我更加坚定地遵守 TDD 原则时,我以后花在返工上的时间就会减少。不过,花费的时间完全取决于您编写单元测试的程度。如果单元测试没有捕捉到预期的行为,那么花在它们上的所有时间都被浪费了。

