Screw diamonds. Ruby is a girl's best friend.

Surfing the waters of programming

7 Easy Steps to Start With TDD

| Comments

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.

**TIP**
**spec_helper.rb** will hold all of the required paths for you.
Do not forget to include all necessary files, gem names, and libraries.
_require 'spec_helper'_ will be the only line needed in your testing files.
spec_helper.rb
1
2
3
4
5
6
require 'rspec'
  require_relative '../artist'
  require_relative '../artist'
  require_relative '../song'
  require_relative '../genre'
require 'yaml'
artist.rb
1
require 'spec_helper'

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.

[assert_test] [ruby]
1
2
3
test 'Can initialize an Artist' do
  assert Artist.new
end

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

**TIP**

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

1
rspect artist_spec.rb

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.

[artist.rb] [ruby]
1
2
3
class Artist

end

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???

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

Comments