7 Easy Steps to Start With TDD

Flatiron School / 25 October 2012

The following is a guest post by Jenya Zueva and originally appeared on her blog. Jenya is currently a student a The Flatiron School. You can learn more about her here, or follow her on twitter here.

1:00 pm – TDD is a very useful tool in software development…

3:30 pm – Test driven development or TDD can be extremely frustrating, time consuming and sole devouring experience.

6:00 pm – Be patient. Like a sorority club, TDD will put you through a tidious initiation ceremony. Once, accepted, you will be able to produce a much cleaner code and improve your style.

Few easy steps to get you started with TDD using rspec.

Tools used:

  • Sublime Text 2
  • Terminal
  • gem of choise: rspec
  • original assignment: by Avi Flombaum can be found in my GitHub repo.

Step 1 : Installing rspec

Create a new folder for your project, cd to that folder and run gem install rspec.

Step 2 : Identify what needs to be accomplished

I had a sample of tests completed for Ruby assignment using assert_method. By running this test, I can test if my program is able to create an instance of artist.

Step 3 : Write your test

In artist_spec.rb, I need to be able to test that

  • by calling #new method on class variable @artist
  • I will be able to create a new instance of artist
  • that should be_an_instance_of Artist class.

(What? Yes! It is all one sentence. You don’t like my overly explicit coding syntax? Sorry, get over it.)

Stay on a safe side. Commit your code to a GitHub repository. Here is a great post on how to get started with Git.

Step 4 : Run your test – FAIL

Terminal will tell you exactly what is wrong with your test.

Step 5 : Fix your code

To pass this test we need to initiate class Artist.

Step 6 : Run your test again – PASS

Step 7 : Do a happy dance

Original Work

Here is an example of a more complex test:

Create the test
Test and Fail
Fix your code

Test and Pass

Happy dance anyone???

I plan to transofrm all test cases from original state to rspect. Feel free to fork it here.

What is ERB? Previous Post 5 Great Guides to Help You Get Started With GitHub Next Post