# How to write codes for TDD

**URL:** <https://www.ruby-forum.com/t/how-to-write-codes-for-tdd/192267>\
**Category:** Rails\
**Created:** [July 27, 2010, 1:01pm UTC](https://www.ruby-forum.com/t/how-to-write-codes-for-tdd/192267 "2010-07-27T13:01:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![debadatta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@debadatta](https://www.ruby-forum.com/u/debadatta)\
**Post date:** [July 27, 2010, 1:01pm UTC](https://www.ruby-forum.com/t/how-to-write-codes-for-tdd/192267/1 "2010-07-27T13:01:31Z")

</div>

Hi all,  
I am a newbie in rails. I have done some small applications in  
rails but can’t know how to make TDD and BDD in RSpec and other  
tools. I have tried by reading the rpesc tutorial from rspec.info  
website, But can’t understand where to start coding and run for Red,  
Green behaviour of code. Please help,

Thanks  
deb

---

<div class="post-metadata">

**Author:** ![debadatta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@debadatta](https://www.ruby-forum.com/u/debadatta)\
**Post date:** [July 27, 2010, 1:06pm UTC](https://www.ruby-forum.com/t/how-to-write-codes-for-tdd/192267/2 "2010-07-27T13:06:47Z")

</div>

On Jul 27, 11:59 am, debadatta [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,  
> I am a newbie in rails. I have done some small applications in  
> rails but can’t know how to make TDD and BDD in RSpec and other  
> tools. I have tried by reading the rpesc tutorial from rspec.info  
> website, But can’t understand where to start coding and run for Red,  
> Green behaviour of code. Please help,

The theory is quite simple: instead of writing some code and then a  
test to exercise, write a test first (that sets out what you are  
trying to achieve) and then write the code for it. That flip in  
mindset is somewhat independent to the actual tool use use to write  
your tests (although some encourage it more than others)

Fred

---

<div class="post-metadata">

**Author:** ![debadatta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@debadatta](https://www.ruby-forum.com/u/debadatta)\
**Post date:** [July 27, 2010, 1:20pm UTC](https://www.ruby-forum.com/t/how-to-write-codes-for-tdd/192267/3 "2010-07-27T13:20:15Z")

</div>

> Thanks  
> deb

The Ruby-on-Rails tutorial book ([http://railstutorial.org/](http://railstutorial.org/)) is a good  
starting point. See section Mostly static pages/Our first tests.

Adrian

---

<div class="post-metadata">

**Author:** ![debadatta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@debadatta](https://www.ruby-forum.com/u/debadatta)\
**Post date:** [July 27, 2010, 2:04pm UTC](https://www.ruby-forum.com/t/how-to-write-codes-for-tdd/192267/4 "2010-07-27T14:04:09Z")

</div>

Buy “The RSpec Book” from Pragmatic. It’s outstanding, and will give you  
extremely useful insight on TDD and BDD.

Cheers!!

---

<div class="post-metadata">

**Author:** ![debadatta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@debadatta](https://www.ruby-forum.com/u/debadatta)\
**Post date:** [July 27, 2010, 4:12pm UTC](https://www.ruby-forum.com/t/how-to-write-codes-for-tdd/192267/5 "2010-07-27T16:12:47Z")

</div>

Juan P. Genovese wrote:

> Buy “The RSpec Book” from Pragmatic. It’s outstanding, and will give you  
> extremely useful insight on TDD and BDD.

Take this really simple example of a spec (test) from “The RSpec Book.”

## greeter\_spec.rb

## describe “RSpec Greeter” do it “should say ‘Hello RSpec!’ when it receives the greet() message” do greeter = RSpecGreeter.new greeting = greeter.greet greeting.should == “Hello RSpec!” end end

Now run that file with:

spec greeter\_spec.rb

Obviously it will fail. There is no code to implement RSpecGreeter  
class. You now have a successful failure. Failure is what you expected  
at this point.

You are now at “Red.”

You’re next goal is to get to “Green.” You do that by implementing just  
enough code to make the above spec pass. This involves creating the  
RSpecGreeter class and implementing a “greet” method that returns the  
string “Hello RSpec!” This is exactly what the spec says to implement.  
Nothing more, nothing less.

Now run the spec again, which should now pass. You are at “Green.”

Next step is “Refactor.” This is your opportunity to review the code you  
wrote and clean it up by removing duplication, improving formatting,  
etc. You can now do that refactoring with confidence because you have  
the spec that will tell you if you’ve broken anything, according to the  
specified requirement.

The next step in the process is to get back to “Red” by writing another  
spec (or test).

This is a basic BDD cycle. Keep repeating this cycle until you can’t  
think of any more specs to write. At that point you should have  
something to release (or at least be able to complete a feature story).
