Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Is my assumption of test driven development correct?

I am a little confused on how Test driven development works. My assumption is that when doing Test driven development, we write tests to make sure a part of our app works how we want it to work, and if it fails, we write the code to make it pass.

Take the test below for example. What does each line in the test do?

test "a user should have a unique profile name" do
    user = User.new
    user.profile_name = users(:someguy).profile_name        
        assert !user.save
    assert !user.errors[:profile_name].empty?
end

Thanks for your help!

2 Answers

Brian MacDonald
Brian MacDonald
4,951 Points

Hello Joel!

You are definitely not wrong in how you see it.

Think of test driven development as being a proactive approach to a situation, rather than reacting as the issues occur.

Assuming all your prep work is done (planing the app, what is should do, what will or can go wrong, etc) your next step in TDD would be to add your first test, run your test, if failure make small changes, run again, fail make another small change. Once you have made minimal changes with compliant code you move on in development until all steps are given a green light.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Brian explained TDD, so I have nothing to add to it, so let me explain the code you asked about:

test "a user should have a unique profile name" do - this is the description of the test

user = User.new - this creates a new object of class User and names the object user

user.profile_name = users(:someguy).profile_name - user's profile_name is set to the profile name of another, existing user

assert !user.save - we assert that our object user will not be saved to the database, because his name is the same as the name of another user that is already in the database

assert !user.errors[:profile_name].empty? - we assert that there will be errors associated with profile_name when trying to save our user in the database

Unfortunately, this code is not as readable as RSpec code, that's why a lot of Rails developers don't use the default testing framework and instead use the RSpec gem for tests (Treehouse's Todo List application course uses RSpec if you want to see it in action).

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

All that being said - about TDD - you should definitely watch the discussions on "Is TDD dead?" where the creator of Rails shows his point of view - sometimes you have no idea how you will write your application, so you may write your tests AFTER coding and there's nothing wrong with it, since the goal was reached - an application with good tests that document it and allow you to catch regressions when adding features.