测试rails应用程序的最佳方法是什么?

时间:2022-09-15 20:50:52

I am new to testing in Rails. I have decided to learn Test Driven Development and hence I'm researching on how to go about it in rails.

我是Rails测试的新手。我已经决定学习测试驱动开发,因此我正在研究如何在轨道中实现它。

I started out with Test::Unit and soon came to know that better tools and testing frameworks available. Things I have heard of are

我从Test :: Unit开始,很快就知道可以使用更好的工具和测试框架。我听说过的是

  1. Shoulda
  2. Mocha
  3. Rspec
  4. Cucumber
  5. Factory Girl

Now I am very confused as to how to go ahead. What is the best combination of these tools I need to learn? Also where can I find resources to learn these?

现在我很困惑如何继续前进。我需要学习的这些工具的最佳组合是什么?我在哪里可以找到学习这些资源的资源?

I am building the app in Rails 3.0

我在Rails 3.0中构建应用程序

3 个解决方案

#1


1  

Personally I use Cucumber, RSpec and Factory Girl in my applications. As for a resource, I'm writing a book called Rails 3 in Action which goes through using these tools (and Rails itself, of course) to develop an application.

我个人在我的应用程序中使用Cucumber,RSpec和Factory Girl。至于资源,我正在写一本名为Rails 3 in Action的书,它通过使用这些工具(当然还有Rails本身)来开发一个应用程序。

#2


18  

What is the best way to test a rails app?

测试rails应用程序的最佳方法是什么?

Test first.

More seriously, you've listed a number of different tools which are designed to do different things.

更严重的是,您列出了许多不同的工具,这些工具旨在执行不同的操作。

  • Shoulda is a test framework for running tests and a set of matchers and assertions for writing tests.
  • Shoulda是用于运行测试的测试框架,以及用于编写测试的一组匹配器和断言。

  • Mocha is a mocking and stubbing library.
  • 摩卡是一个模拟和存根库。

  • Rspec is a test framework which includes mocking tools and matchers.
  • Rspec是一个包含模拟工具和匹配器的测试框架。

  • Cucumber is a framework for writing functional tests.
  • Cucumber是编写功能测试的框架。

  • Factory Girl is a framework for building the domain objects you use in your tests.
  • Factory Girl是用于构建测试中使用的域对象的框架。

Of those either Shoulda or Rspec might replace Test::Unit while the others all provide tools for different areas of testing. Which one you need depends on what you think will make writing tests easier and more effective.

其中,Shoulda或Rspec可能取代Test :: Unit,而其他都为不同的测试领域提供工具。您需要哪一个取决于您认为编写测试更容易和更有效的方法。

For someone used to Test::Unit unit tests and interested in learning additional tools I would suggest the following.

对于习惯于测试::单元测试且有兴趣学习其他工具的人,我建议如下。

If you have an existing project using Test::Unit consider adding Mocha's mocking and stubbing and see if that allows you to write focused unit tests more easily. If you already have fixtures in place consider using Factory Girl to generate factories instead as you add new model objects and again see if you find that makes your tests easier to manage and maintain. If you have a solid set of unit tests in place already consider using Cucumber to write some higher level functional tests to begin capturing user stories in tests and testing your app end to end.

如果您有一个使用Test :: Unit的现有项目,请考虑添加Mocha的模拟和存根,看看是否允许您更轻松地编写有针对性的单元测试。如果您已经安装了夹具,请考虑使用Factory Girl来生成工厂,而不是添加新的模型对象,并再次查看是否发现这样可以使您的测试更易于管理和维护。如果你有一套可靠的单元测试已经考虑使用Cucumber编写一些更高级别的功能测试来开始在测试中捕获用户故事并在端到端测试你的应用程序。

If you are starting a new project then start with Rspec and Factory Girl. Rspec will introduce you to a very different style of testing than Test::Unit but you'll still be writing fairly familiar unit tests and it provides stubs, mocks, and matchers which will let you dive into testing without an explosion of too many new gems in your project. Factory Girl will give you a convenient way to build domain objects as your tests require them. Between those two you should be able to pick up a bunch of new testing tricks while still working with your usual unit testing workflow.

如果您要开始一个新项目,那么请从Rspec和Factory Girl开始。 Rspec将为您介绍一种与Test :: Unit完全不同的测试方式,但您仍然会编写相当熟悉的单元测试,并提供存根,模拟和匹配器,这将让您深入测试而不会爆发太多新的测试你项目中的宝石。 Factory Girl将为您提供一种方便的方法来构建域对象,因为您的测试需要它们。在这两者之间,您应该能够获得一堆新的测试技巧,同时仍然使用您通常的单元测试工作流程。

#3


3  

Have you checked the Michael Hartl's RoR 3 tutorial?

你检查了Michael Hartl的RoR 3教程吗?

Ruby on Rails 3 Tutorial by Michael Hartl

Ruby Hart的Ruby on Rails 3教程

He has integrated a good Rails 3 tutorial with some basics usage of RSpec and Factory Girl.

他已经整合了一个很好的Rails 3教程,以及RSpec和Factory Girl的一些基本用法。

#1


1  

Personally I use Cucumber, RSpec and Factory Girl in my applications. As for a resource, I'm writing a book called Rails 3 in Action which goes through using these tools (and Rails itself, of course) to develop an application.

我个人在我的应用程序中使用Cucumber,RSpec和Factory Girl。至于资源,我正在写一本名为Rails 3 in Action的书,它通过使用这些工具(当然还有Rails本身)来开发一个应用程序。

#2


18  

What is the best way to test a rails app?

测试rails应用程序的最佳方法是什么?

Test first.

More seriously, you've listed a number of different tools which are designed to do different things.

更严重的是,您列出了许多不同的工具,这些工具旨在执行不同的操作。

  • Shoulda is a test framework for running tests and a set of matchers and assertions for writing tests.
  • Shoulda是用于运行测试的测试框架,以及用于编写测试的一组匹配器和断言。

  • Mocha is a mocking and stubbing library.
  • 摩卡是一个模拟和存根库。

  • Rspec is a test framework which includes mocking tools and matchers.
  • Rspec是一个包含模拟工具和匹配器的测试框架。

  • Cucumber is a framework for writing functional tests.
  • Cucumber是编写功能测试的框架。

  • Factory Girl is a framework for building the domain objects you use in your tests.
  • Factory Girl是用于构建测试中使用的域对象的框架。

Of those either Shoulda or Rspec might replace Test::Unit while the others all provide tools for different areas of testing. Which one you need depends on what you think will make writing tests easier and more effective.

其中,Shoulda或Rspec可能取代Test :: Unit,而其他都为不同的测试领域提供工具。您需要哪一个取决于您认为编写测试更容易和更有效的方法。

For someone used to Test::Unit unit tests and interested in learning additional tools I would suggest the following.

对于习惯于测试::单元测试且有兴趣学习其他工具的人,我建议如下。

If you have an existing project using Test::Unit consider adding Mocha's mocking and stubbing and see if that allows you to write focused unit tests more easily. If you already have fixtures in place consider using Factory Girl to generate factories instead as you add new model objects and again see if you find that makes your tests easier to manage and maintain. If you have a solid set of unit tests in place already consider using Cucumber to write some higher level functional tests to begin capturing user stories in tests and testing your app end to end.

如果您有一个使用Test :: Unit的现有项目,请考虑添加Mocha的模拟和存根,看看是否允许您更轻松地编写有针对性的单元测试。如果您已经安装了夹具,请考虑使用Factory Girl来生成工厂,而不是添加新的模型对象,并再次查看是否发现这样可以使您的测试更易于管理和维护。如果你有一套可靠的单元测试已经考虑使用Cucumber编写一些更高级别的功能测试来开始在测试中捕获用户故事并在端到端测试你的应用程序。

If you are starting a new project then start with Rspec and Factory Girl. Rspec will introduce you to a very different style of testing than Test::Unit but you'll still be writing fairly familiar unit tests and it provides stubs, mocks, and matchers which will let you dive into testing without an explosion of too many new gems in your project. Factory Girl will give you a convenient way to build domain objects as your tests require them. Between those two you should be able to pick up a bunch of new testing tricks while still working with your usual unit testing workflow.

如果您要开始一个新项目,那么请从Rspec和Factory Girl开始。 Rspec将为您介绍一种与Test :: Unit完全不同的测试方式,但您仍然会编写相当熟悉的单元测试,并提供存根,模拟和匹配器,这将让您深入测试而不会爆发太多新的测试你项目中的宝石。 Factory Girl将为您提供一种方便的方法来构建域对象,因为您的测试需要它们。在这两者之间,您应该能够获得一堆新的测试技巧,同时仍然使用您通常的单元测试工作流程。

#3


3  

Have you checked the Michael Hartl's RoR 3 tutorial?

你检查了Michael Hartl的RoR 3教程吗?

Ruby on Rails 3 Tutorial by Michael Hartl

Ruby Hart的Ruby on Rails 3教程

He has integrated a good Rails 3 tutorial with some basics usage of RSpec and Factory Girl.

他已经整合了一个很好的Rails 3教程,以及RSpec和Factory Girl的一些基本用法。