Rails 4 Test Prescriptions

Book description

Does your Rails code suffer from bloat, brittleness, or inaccuracy? Cure these problems with a regular dose of test-driven development. Rails 4 Test Prescriptions is a comprehensive guide to how tests can help you design and write better Rails applications. In this completely revised edition, you'll learn why testing works and how to test effectively using Rails 4, Minitest 5, and RSpec 3, as well as popular testing libraries such as factory_girl and Cucumber. Do what the doctor ordered to make your applications feel all better. Side effects may include better code, fewer bugs, and happier developers.

Table of contents

  1. Rails 4 Test Prescriptions
    1. Copyright
    2. For the Best Reading Experience...
    3. Table of Contents
    4. Early praise for Rails 4 Test Prescriptions
    5. Acknowledgments
    6. Chapter 1: Introduction
      1. A Test-Driven Fable
      2. Who You Are
      3. Testing First Drives Design
      4. What Is TDD Good For?
      5. When TDD Needs Some Help
      6. Words to Live By
      7. A Word About Tools, Best Practices, and Teaching TDD
      8. Coming Up Next
      9. Changes in the Second Edition
    7. Chapter 2: Test-Driven Development Basics
      1. Infrastructure
      2. The Requirements
      3. Installing RSpec
      4. Where to Start?
      5. Running Our Test
      6. Making Our Test Pass
      7. The Second Test
      8. Back on Task
      9. Adding Some Math
      10. Our First Date
      11. Using the Time Data
      12. What We’ve Done
    8. Chapter 3: Test-Driven Rails
      1. And Now Let’s Write Some Rails
      2. The Days Are Action-Packed
      3. Who Controls the Controller?
      4. A Test with a View
      5. What Have We Done? And What’s Next?
    9. Chapter 4: What Makes Great Tests
      1. The Big One
      2. The Big Two
      3. The More Detailed Five: SWIFT Tests
      4. Using SWIFT Tests
    10. Chapter 5: Testing Models
      1. What Can We Do in a Model Test?
      2. What Should I Test in a Model Test?
      3. Okay, Funny Man, What Makes a Good Set of Model Tests?
      4. Refactoring Models
      5. A Note on Assertions per Test
      6. Testing What Rails Gives You
      7. Testing ActiveRecord Finders
      8. Testing Shared Modules and ActiveSupport Concerns
      9. Write Your Own RSpec Matchers
      10. Modeling Data
    11. Chapter 6: Adding Data to Tests
      1. What’s the Problem?
      2. Fixtures
      3. Factories
      4. Dates and Times
      5. Fixtures vs. Factories vs. Test Doubles
    12. Chapter 7: Using Test Doubles as Mocks and Stubs
      1. Mock Objects Defined
      2. Creating Stubs
      3. Mock Expectations
      4. Using Mocks to Simulate Rails Save
      5. Using Mocks to Specify Behavior
      6. More Expectation Annotations
      7. Mock Tips
    13. Chapter 8: Testing Controllers and Views
      1. Testing Controllers
      2. Simulating Requests in a Controller Test
      3. Evaluating Controller Results
      4. Testing Routes
      5. Testing Helper Methods
      6. Testing Views and View Markup
      7. Presenters
      8. Testing Mailers
      9. Managing Controller and View Tests
    14. Chapter 9: Minitest
      1. Getting Started with Minitest
      2. Minitest Basics
      3. Running Minitest
      4. Minitest and Rails Controllers
      5. Minitest and Views
      6. Minitest and Routing
      7. Minitest Helper Tests
      8. Mocha
      9. Onward
    15. Chapter 10: Integration Testing with Capybara and Cucumber
      1. What to Test in an Integration Test
      2. Setting Up Capybara
      3. Outside-in Testing
      4. Using Capybara
      5. Making the Capybara Test Pass
      6. Retrospective
      7. Trying Cucumber
      8. Setting Up Cucumber
      9. Writing Cucumber Features
      10. Writing Cucumber Steps
      11. More-Advanced Cucumber
      12. Is Cucumber Worth It?
      13. Looking Ahead
    16. Chapter 11: Testing for Security
      1. User Authentication and Authorization
      2. Adding Users and Roles
      3. Restricting Access
      4. More Access Control Testing
      5. Using Roles
      6. Protection Against Form Modification
      7. Mass Assignment Testing
      8. Other Security Resources
    17. Chapter 12: Testing External Services
      1. External Testing Strategy
      2. Our Service Integration Test
      3. Introducing VCR
      4. Client Unit Tests
      5. Why an Adapter?
      6. Adapter Tests
      7. Testing for Error Cases
      8. Smoke Tests and VCR Options
      9. The World Is a Service
    18. Chapter 13: Testing JavaScript
      1. Unit-Testing JavaScript
      2. Our Real Jasmine Project
      3. Testing Ajax Calls
      4. Integration Testing with Capybara and JavaScript
      5. JavaScript Fiddle
    19. Chapter 14: Troubleshooting and Debugging
      1. General Principles
      2. The Humble Print Statement
      3. Git Bisect
      4. Pry
      5. Really Common Rails Gotchas
    20. Chapter 15: Running Tests Faster and Running Faster Tests
      1. Running Smaller Groups of Tests
      2. Guard
      3. Running Rails in the Background
      4. Writing Faster Tests by Bypassing Rails
      5. Recommendations for Faster Tests
    21. Chapter 16: Testing Legacy Code
      1. What’s a Legacy?
      2. Set Expectations
      3. Getting Started with Legacy Code
      4. Test-Driven Exploration
      5. Dependency Removal
      6. Find the Seam
      7. Don’t Look Back
    22. Bibliography
      1. You May Be Interested In…

Product information

  • Title: Rails 4 Test Prescriptions
  • Author(s): Noel Rappin
  • Release date: December 2014
  • Publisher(s): Pragmatic Bookshelf
  • ISBN: 9781941222195