Test Driven Development in RoR
RoR Testing February 12th, 2008Here’s TDD- new code developement technology in this rails jungle that make the business processes clearer for developers …..
Here we use the test to drive the code…..not the code to make the tests crazy..
Just starting of with a few points for u here which will track u down on the base of TDD
Test Driven Design
aka “Test First”
Drives design desisions through testing.
Ensures things are testable.•
Provides a fast and tight feedback loop.
Attacks the problem at the specification level.
Doesn’t wait until the last minute to see if things work.
TDD Mantra On The Go
•Red - produce failing testcase
•Failures should make you happy!
•Go Green - implement and pass test
•Refactor - clean up - made safer by previous two steps
•Repeat until done
Lets Think Practically:
Im using a simple bank account example for the TDD clearence in u r minds
class AccountTest < Test::Unit::TestCase
def test_new_account_should_have_zero_balance
account = Account.new
assert_equal 0, account.balance
end
end
Its time to run the test:
E 1) Error: test_new_account_should_have_zero_balance:
NameError: uninitialized constant Account ./test/unit/account_test.rb:6:in `test_new_account_should_have_zero_balance’
1 tests, 0 assertions, 0 failures, 1 errors
Oppsss..got a name error… yahh..i got it.. i think i have to play with migrations now… since our failing test is crying taking the name error
class CreateAccount < ActiveRecord::Migration
def self.up create_table “accounts”, :force => true do |t|
t.column :balance, :integer, :default => 0
end
end
end
We will leave the model as it is for the timebeing….lets not play with it…
class Account < ActiveRecord::Base
end
Then we run the test again to make is pass…lets see…
Started . Finished in 0.055268 seconds.
1 tests, 1 assertions, 0 failures, 0 errors
Hurray…that looks pretty good…
Lets depoesit some money into the account…we write the test first..
class DepositTest < Test::Unit::TestCase
def test_should_update_balance_when_making_deposit
account = Account.new account.deposit(10)
assert_equal 10, account.balance
end
run the test…
E 1) Error: test_should_update_balance_when_making_deposit(DepositTest): NoMethodError: undefined method `deposit’ for #
./test/unit/deposit_test.rb:8:in`test_should_update_balance_when_making_deposit’
2 tests, 1 assertions, 0 failures, 1 errors
Oh..Yeahh..It failed again….
The model comes into the picture now:
class Account < ActiveRecord::Base
def deposit(amount)
end
end
Just a little change …and we run the test back again..
1) Failure: test_should_update_balance_when_making_deposit:
<10> expected but was
<0>.
2 tests, 2 assertions, 1 failures, 0 errors
Ohhh…My assertion failed….I think I need to add more in the model…
class Account < ActiveRecord::Base
def deposit(amount)
self.balance += amount #adding the amount to the existing balance
end
end
Finally run the test again….It should pass everything…
Started..Finished in 0.242602 seconds.
2 tests, 2 assertions, 0 failures, 0 errors
Yup…Im on TDD…Its really that simple…!
Moral of the story:
- With TDD u can can have a total control over your code and application working..
- We really dont need any testers banging their head doing out some testing on a full proof code developed through TDD technique…
- Enhances better code refactoring methods and also assures better code coverage for the tests…








