Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to test a rails app?

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.

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

  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

like image 319
Avinasha Shastry Avatar asked Jan 07 '11 04:01

Avinasha Shastry


1 Answers

What is the best way to test a rails app?

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.
  • Mocha is a mocking and stubbing library.
  • Rspec is a test framework which includes mocking tools and matchers.
  • Cucumber is a framework for writing functional tests.
  • Factory Girl is a framework for building the domain objects you use in your tests.

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.

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.

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.

like image 177
Jonah Avatar answered Oct 14 '22 15:10

Jonah