Archive for November, 2007

Testing in Rails: Part 4 - Unit Testing in Rails

Thursday, November 29th, 2007

This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with the introduction and overview of the ideas behind testing.

Unit Tests + Rails

After so much discussion of unit testing in Ruby you are probably anxious to start applying your new-found skills to a Rails application. The good news is that everything you have learned so far is directly applicable to testing in Rails.

When you first create a new Rails application (e.g. rails sample_app), a folder called ‘test’ is created in your application’s root directory. This is the folder where all of your test code will reside.

Test Folder

As you would expect, we will be putting all of our application’s unit tests inside the subfolder called ‘unit’. At first that folder will be empty. When a new model is generated, the generator script will also create a skeleton for unit testing that model.

(more…)

Testing in Rails: Part 3 - Unit Testing Ruby Classes (cont.)

Tuesday, November 20th, 2007

This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with the introduction and overview of the ideas behind testing.

Testing the colors Method

Now that we have examined the thought process behind writing tests, I will move more quickly through test_colors and test_full_name. test_colors is simple: invoking colors on the class Car will return an array of colors. How much and what kind of testing this requires is up to you and really depends on your application’s usage. Here are some examples:

def test_colors
    assert(Car.colors.kind_of?(Array))
    assert_equal(4, Car.colors.length)
    assert_equal(['blue', 'black', 'red', 'green'], Car.colors)
    assert(Car.colors.include?(@volvo.color))
    assert(Car.colors.include?(@honda.color))
    assert(!Car.colors.include?(@dodge.color))
    assert(!Car.colors.include?(Car.new.color))
  end

(more…)

Testing in Rails: Part 2 - Unit Testing Ruby Classes

Saturday, November 17th, 2007

Cars

This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with the introduction and overview of the ideas behind testing.

Unit Testing Classes and Instances

Any Ruby code can be tested with TestUnit. Working with classes and instances of objects is no different. Let’s create definition for a Car class (keeping with the car theme from the last section). Then we can write tests that will test the Car class. We will also spend some time considering what we should test and how we can test it.

(more…)

Testing in Rails: Part 1 - Unit Testing in Ruby

Wednesday, November 14th, 2007

This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with the introduction and overview of the ideas behind testing.

Unit Testing

Volt gauge

I am going to cover several types of tests over the course of this tutorial: unit tests, functional tests, integration tests. I will start with unit tests. Unit tests are a good way to introduce the subject of testing and, if you already have a working Rails application, they are the first tests you should consider adding. Think of unit tests as our first testing layer, a foundation for the other layers.

Unit testing is a testing technique to validate that individual “units” of code are working properly. A “unit” refers to the smallest testable part of the code. When unit testing, we are not interested in how all the units fit together, just in make sure that each unit is functioning properly on its own.

(more…)

Testing in Rails: Introduction

Saturday, November 10th, 2007

Meter face

For some time, I’ve been wanting to teach how to get started writing tests for Ruby on Rails. Eventually I’d like to do a video training on the subject, but until then I’ve decided to write a series of blog posts as a tutorial.

Everyone agrees that testing is important, yet a small percentage of developers actually write tests. That’s understandable because there is a lot to learn when first starting with Ruby on Rails: installation, Ruby, the MVC framework, Rails, ActiveRecord, migrations, validations, routes, pagination, plug-ins, deployment… There’s more than enough to keep beginners busy during their first few projects. Testing requires learning yet another skill for something that seems like it can be put off until later. After all, you just want to get the site built, right?

Experienced developers often preach to beginners how important it is to test. I’ve even heard it said: “You are not a professional developer unless you write tests.” That is simply not true. You can write great applications, have lots of clients and make lots of money without writing tests. There are thousands of professional developers who don’t. But what I think they really mean by that statement is: Once you start writing tests, you’ll reap a lot of benefits (saving time and writing better code), take your programming to another level, and have a competitive edge over developers who don’t write tests. And who doesn’t want to save time and write better code???

But how do you get there? If you are a Ruby on Rails beginner, focus on the essentials first. Write an application or two. You can start writing tests from the beginning, but I think it’s best if you learn to program in Ruby and have a solid understanding of what Rails is doing first. After you have the fundamentals down, you’ll be ready to learn to write tests. Don’t worry, it’s not as difficult as it might seem. I’m going to walk you through the process step-by-step. (It’ll be an ongoing project spread out over many blog posts.)

(more…)