Test Driven Development In Rails — Part 1

Tests and Fixtures

I look pretty haggard in this video. I swear it’s the camera and the lighting!

Introduction to Testing

Programmers are always testing their code. Tests are written to ensure that the code does what it is expected to do.

There are two types of testing:

Manual Testing

Running the code and role playing as the user. In the case of a Rails app, this means using the app in the browser.

Automated Testing

Using software to run tests and verifying the results. For this tutorial, we will be using MiniTest.

Test Driven Development

Test Driven Development is a code testing philosophy. It consists of a development process, based on a very simple and robust principle. Developers make a list of requirements their code must produce. These requirements become specific test cases. The test is written, then the code base is modified for the test to pass.

In this tutorial, we are going to build a very simple app bulletin board type app. Users will make posts that appear on a wall. Let’s come up with some user stories to give us some direction, but keep it very simple for now so we can focus on learning about tests.

User Stories

  • A user should be able to submit a post with a title and a body.
  • A user should be able to see the posts on the posts#index page.

The practise of TDD requires that we guide development by writing the tests first. Let’s make a list of some very simple tests just to get us started. We will modify the list of user stories or tests as required. This is the streamlined, simple nature of TDD.

Post Model Tests

  • Should be invalid without a title.
  • Should be invalid without a body.

Post Controller Tests

  • Gets posts#index
  • Gets posts#new

That should be enough for now, let’s get started.

Follow Daniel Pereira on Twitter