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.
1 2 3 4 5 6 |
|
1
|
|
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.
1 2 3 |
|
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
|
|
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.
1 2 3 |
|
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.