Chapter 5 - Getting started with testing

Introduction

If you've spent much time developing web applications, you've no doubt experienced the pain of making a form handler (for, say, registration), filling it out to test it, making some changes (say, adding some fields), filling it out again, having it break, filling it out again, fixing one problem but having it break for a second unrelated reason, filling it out again—well, you get the idea. What a disaster! What's a developer to do?

Rails to the rescue! By using automated testing we don't have to do any of those things by hand—Rails does them all for us. Rails unit tests let us check our model validations and make sure that the database is working. Functional tests let us simulate a browser hitting the controller actions so we can verify responses, redirects, variable assignments, and HTML tags. Finally, integration tests let us see how different parts of the application interact by simulating a browser hopping from page to page. In this chapter, we cover unit and functional tests; we introduce integration testing in Section 7.4.

By taking time now to write tests for our models, views, and controllers, we effectively stop moving forward with our application and instead take a step sideways. If you're skeptical that this is a good idea, you're not alone (see the sidebar "Aure says: Don't panic!"). In our experience, though, tests make for better, cleaner code, and end up saving time in the long run by exposing flaws in the current application and catching bugs as the application evolves. Put simply, the Rails testing facilities are the greatest thing since sliced bread.

Table of Contents

  • 5.1 Our testing philosophy 98
  • 5.2 Test database configuration 98
  • 5.3 Site controller testing 99
    • 5.3.1 A nontrivial test 100
    • 5.3.2 Test overkill? 103
  • 5.4 Registration testing 103
    • 5.4.1 Running functional tests 104
    • 5.4.2 Basic registration tests 104
    • 5.4.3 Testing successful registration 107
    • 5.4.4 Testing unsuccessful registration 108
    • 5.4.5 Running the tests 110
    • 5.4.6 More registration tests? 111
  • 5.5 Basic User model testing 111
    • 5.5.1 Basic validation testing 113
  • 5.6 Detailed User model testing 115
    • 5.6.1 Testing uniqueness 115
    • 5.6.2 Testing screen name length 117

Source Code

Errata

As of the first printing, these are the known corrections:

  1. p. 102. etitle should be @title
  2. p. 105, 108. <legend>Register</legend> in the registration page HTML source views is inconsistent with the registration page from Chapter 4, which has instead <legend>Enter Your Details</legend>.
  3. p. 108. The screen name error in the HTML output is wrong. Instead of
          Screen name is too short (minimum is 4 characters)
        
    it should be
          Screen name must contain only letters, numbers, and underscores  
        
  4. p. 112. At the bottom of the first paragraph, "users(:second)" should be "users(:another)".
  5. p. 119. All occurrences of the word "maximum" within the console output at the top should be "minimum".

Screencast

LESSON 5: Getting Started with Testing