# Rails and TDD

**URL:** <https://www.ruby-forum.com/t/rails-and-tdd/82246>\
**Category:** Rails\
**Created:** [January 18, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/rails-and-tdd/82246 "2007-01-18T15:20:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![technodolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@technodolt](https://www.ruby-forum.com/u/technodolt)\
**Post date:** [January 18, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/rails-and-tdd/82246/1 "2007-01-18T15:20:34Z")

</div>

I’m trying out the whole TDD on my newest project, and here’s my  
dilemma:

I am creating a login system (I know, I know… re-inventing the wheel,  
blah blah blah; I’m doing this to teach myself things, not because I  
don’t think there’s something out there that can handle what I want),  
and so far, I’ve realized I’m going to need a User model and a User  
controller. I’ve created both of them, and I’m starting to write the  
tests that I will be developing towards passing: my question is, should  
I write tests for the controller, then the model; the model, then the  
controller; or try to do them both in tandem? I’m having problems  
figuring out where to start with this, as I’ve never done TDD before and  
I’m fairly new to the Rails game in general.

---

<div class="post-metadata">

**Author:** ![technodolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@technodolt](https://www.ruby-forum.com/u/technodolt)\
**Post date:** [January 18, 2007, 3:23pm UTC](https://www.ruby-forum.com/t/rails-and-tdd/82246/2 "2007-01-18T15:23:53Z")

</div>

Do the model first, since the controller will depend on the model.

–  
Building an e-commerce site with Rails?  
[http://www.agilewebdevelopment.com/rails-ecommerce](http://www.agilewebdevelopment.com/rails-ecommerce)

Meet up at RailsConf:  
[http://railsconf2007.conferencemeetup.com/](http://railsconf2007.conferencemeetup.com/)

---

<div class="post-metadata">

**Author:** ![technodolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@technodolt](https://www.ruby-forum.com/u/technodolt)\
**Post date:** [January 18, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/rails-and-tdd/82246/3 "2007-01-18T16:02:18Z")

</div>

I did this exact same exercise when I was learning rails and it’s a  
GREAT way to pick up both TDD and Rails because down the line you’ll  
be able to compare your results against some of the login generators  
out there and see where you and the others differ.

It’ll also give you a thorough understanding of your login system  
which sadly, if generated, can be a hole in someone’s intellectual  
command of one of the most crucial pieces of their application.

For learning TDD I suggest checking out Geoffrey Grosenbach’s peep cast  
on it:  
[http://peepcode.com/products/test-first-development](http://peepcode.com/products/test-first-development)

Oh and also if you want a headstart on the login system Chad F.'s  
Rails Recipes has some nuggets for both authentication and  
authorization.

I haven’t viewed this particular peepcast but I’ve watched the others  
and they are excellent.

Good luck,

Tim C.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![technodolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@technodolt](https://www.ruby-forum.com/u/technodolt)\
**Post date:** [January 18, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/rails-and-tdd/82246/4 "2007-01-18T17:04:19Z")

</div>

On 1/18/07, Luke I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m trying out the whole TDD on my newest project, and here’s my  
> dilemma:
> 
> I am creating a login system

Break this feature up into tasks, each representing one user  
interaction. Example:

- logout
- login bad user name
- log in bad password
- log in first time

For each one, write enough Railage to enable only that interaction.  
Each one may require tweaks to a database, model, controller, and/or  
view.

Write test cases before as many tweaks as possible. Pass all tests  
after every few edits. If you can’t think of a test (look up  
assert\_select and submit\_form to test the view), then at least add a  
test after you write the code

> (I know, I know… re-inventing the wheel,

Hardly. Many erudite, educated, senior, overpaid, world-class  
programmers still don’t TDD, and anyone who does will easily blow them  
away…

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![technodolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@technodolt](https://www.ruby-forum.com/u/technodolt)\
**Post date:** [January 18, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/rails-and-tdd/82246/5 "2007-01-18T17:20:35Z")

</div>

Definitely test the model first. Check out Rcov as well. It’s a great  
way to help make sure your tests are complete. It won’t make sure  
you’re testing _every_ possible scenario, but it’s a great start.
