如何开始使用 Ruby on Rails 进行 TDD? [英] How to get started on TDD with Ruby on Rails?

查看:35
本文介绍了如何开始使用 Ruby on Rails 进行 TDD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉这些概念(在大学上过测试课程),但我不确定如何真正使用它们,因为我从未参与过真正的"TDD 项目.

我即将开始使用 Ruby on Rails(很可能使用 2.3)开发一个项目.此应用程序将用于管理数据、用户和一些文件.一开始不会太复杂,但在接下来的 6 个月内可能会扩展很多,所以我觉得现在是深入研究 TDD 的合适时机.

我对如何去做有一个基本的想法,但我仍然需要一些指导和建议:

  • Ruby on Rails TDD 101 文章我应该阅读吗?

  • 我需要测试什么?

  • 我应该使用什么 gem/插件?

  • 我应该使用 rspec 吗?还有什么?

  • 获得所有测试类后,我该如何部署它们?(例如:持续集成)

  • TDD 到底有多耗时?

  • 我是否需要阅读有关这方面的书,或者我可以通过玩弄它并阅读在线教程来获得所有信息?如果我需要读一本书,读什么书?

<小时>

我喜欢通过示例学习,所以有人可以告诉我我将如何采用 TDD 方法来解决这个问题:

<块引用>

我有公司.我有联系人.一种联系人可以链接到 1 个公司.一种公司可以有多个联系人.一世想创造创造的方式联系人、公司和链接联系人给公司.

您不必在答案中使用此示例,但它会有所帮助:)

解决方案

我应该阅读什么 Ruby on Rails TDD 101 文章?

我将从测试 rails 应用程序的指南开始.

还有 Railscast 有一些关于如何使用不同测试工具的优秀截屏视频.><块引用>

我需要测试什么?

我将从模型开始,因为它们易于测试.简单的规则是您需要覆盖测试中的每个 if 语句.

您应该测试该方法的用途(以确保它按预期运行)以及所有边缘情况.

还要确保您不会过度测试.

<块引用>

我应该使用什么 gem/插件?我应该使用 rspec 吗?还有什么?

开始时,只需使用 测试单元.您可以使用 rspeccucumber 熟悉基础知识后.

Autotest 是一个很好的工具,如果你想的话真正的测试驱动.但这是一个不需要的'好东西'.

<块引用>

一旦我完成了所有的测试类,我该如何去部署它们?

不确定这个问题.您通常不会部署测试.一旦你有了所有的测试类,简单地输入rake test"来运行你的所有测试.

<块引用>

TDD 到底有多耗时?

真的很节省时间.如果你喜欢迷宫拼图,你就会知道如果你从头到尾解决它几乎总是更容易.与 TDD 相同.如果没有测试驱动,你会一直在思考我接下来应该做什么".使用测试驱动,测试会告诉你接下来要做什么(如果逻辑不存在,它就会中断,所以你只需要修复损坏的部分).此外,您的错误更少,从长远来看,这将为您节省大量时间.

<块引用>

我是否需要阅读有关此的书或我可以通过玩游戏获得一切吗随身携带并在线阅读教程?如果我需要看书,什么书?

你不需要一本书.学习任何东西最有效的方法是:就去做吧.一旦遇到问题或问题,请返回本书或在线资源.这也很敏捷.

在您的示例中,需要测试的内容是:一个联系人可以链接到 1 个公司,一个公司可以有多个联系人,创建创建联系人的方法,以及将联系人链接到公司.

class CompanyTest 

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.

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.

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?


I like learning with examples so could someone tell me how I would go and take a TDD approach to solve this issue:

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.

You don't have to use this example in your answer but it would help :)

解决方案

What Ruby on Rails TDD 101 article should I read?

I will start with a guide to testing rails applications.

Also Railscast has some excellent screencasts about how to use different testing tools.

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.

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?

When you start, just use Test Unit. You can use rspec or cucumber after you get familiar with the basics.

Autotest is a nice tool to have if you want to be truly test driven. But it is a 'nice have' not required.

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.

How time consuming TDD really is?

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.

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.

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

这篇关于如何开始使用 Ruby on Rails 进行 TDD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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