How to get started on TDD with Ruby on Rails?

Ruby on-RailsTestingTdd

Ruby on-Rails Problem Overview


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 :)

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

> 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

Solution 2 - Ruby on-Rails

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

http://www.rubyfocus.biz/class_video/2010/07/19/rails_tdd_class_1.html

Solution 3 - Ruby on-Rails

I recommend this book: Ruby on Rails Tutorial. I'm almost done with it. The book uses TDD the whole book. Give it a try!

Solution 4 - Ruby on-Rails

I recommend this book: Agile Web Development with Rails

Solution 5 - Ruby on-Rails

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.

You might also be interested in this Rails guide.

Solution 6 - Ruby on-Rails

I use :

  1. Shoulda and rspec for testing
  2. Mocha for mocking
  3. Factory_girl for factories
  4. parallel_specs for faster testing
  5. metric_fu for code analysis

Solution 7 - Ruby on-Rails

> What gem/plugin should I use?

I've always enjoyed [shoulda][1].

> How time consuming TDD really is?

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.

[1]: http://thoughtbot.com/projects/shoulda "shoulda"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmarcggView Question on Stackoverflow
Solution 1 - Ruby on-Railsez.View Answer on Stackoverflow
Solution 2 - Ruby on-RailsWolfram ArnoldView Answer on Stackoverflow
Solution 3 - Ruby on-RailssivabudhView Answer on Stackoverflow
Solution 4 - Ruby on-RailsJRLView Answer on Stackoverflow
Solution 5 - Ruby on-RailsAndy GaskellView Answer on Stackoverflow
Solution 6 - Ruby on-RailsMikeView Answer on Stackoverflow
Solution 7 - Ruby on-RailsPatrick RobertsonView Answer on Stackoverflow