Rails 5 Test Prescriptions

Book description

Does your Rails code suffer from bloat, brittleness, or inaccuracy? Cure these problems with the regular application of test-driven development. You'll use Rails 5.1, Minitest 5, and RSpec 3.6, as well as popular testing libraries such as factory_girl and Cucumber. Updates include Rails 5.1 system tests and Webpack integration. Do what the doctor ordered to make your applications feel all better. Side effects may include better code, fewer bugs, and happier developers.

Your Ruby on Rails application is sick. Deadlines are looming, but every time you make the slightest change to the code, something else breaks. Nobody remembers what that tricky piece of code was supposed to do, and nobody can tell what it actually does. Plus, it has bugs. You need test-driven development: a process for improving the design, maintainability, and long-term viability of software.

With both practical code examples and discussion of why testing works, this book starts with the most basic features delivered as part of core Ruby on Rails. Once you've integrated those features into your coding practice, work with popular third-party testing tools such as RSpec, Jasmine, Cucumber, and factory_girl. Test the component parts of a Rails application, including the back-end model logic and the front-end display logic. With Rails examples, use testing to enable your code to respond better to future change. Plus, see how to handle real-world testing situations.

This new edition has been updated to Rails 5.1 and RSpec 3.6 and contains full coverage of new Rails features, including system tests and the Webpack-based JavaScript setup.

What You Need:

Ruby 2.4, Rails 5.1

Publisher resources

View/Submit Errata

Table of contents

  1.  Acknowledgments
  2.  Preface
    1. Who You Are
    2. What’s in This Book
    3. What You’ll Need
    4. A Word About Tools, Best Practices, and Teaching TDD
    5. Changes in This Edition
    6. Online Resources
  3. 1. A Test-Driven Fable
    1. Testing First Drives Design
    2. What Is TDD Good For?
    3. When TDD Needs Some Help
    4. What You’ve Done
  4. 2. Test-Driven Development Basics
    1. Infrastructure
    2. The Requirements
    3. Installing RSpec
    4. Where to Start?
    5. Running the Test
    6. Making the Test Pass
    7. The Second Test
    8. Back on Task
    9. Adding Some Math
    10. The First Date
    11. Using the Time Data
    12. What You’ve Done
  5. 3. Test-Driven Rails
    1. Let’s Write Some Rails
    2. The Days Are Action-Packed
    3. Who Controls the Controller?
    4. A Test with a View
    5. Testing for Failure
    6. What You’ve Done
  6. 4. What Makes Great Tests
    1. The Big One
    2. Cost and Value
    3. SWIFT: The Five Qualities of Valuable Tests
    4. What You’ve Done
  7. 5. Testing Models
    1. What Can You Do in a Model Test?
    2. What Should You 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. Writing Your Own RSpec Matchers
    10. What You’ve Done
  8. 6. Adding Data to Tests
    1. What’s the Problem?
    2. Fixtures
    3. Factories
    4. Dates and Times
    5. What You’ve Done
  9. 7. Using Test Doubles as Mocks and Stubs
    1. Test Doubles Defined
    2. Creating Stubs
    3. Mock Expectations
    4. Using Mocks to Simulate Database Failure
    5. Using Mocks to Specify Behavior
    6. More Expectation Annotations
    7. Mock Tips
    8. What You’ve Done
  10. 8. Integration Testing with Capybara and Cucumber
    1. A Field Guide to Integration and System Tests
    2. Setting Up Capybara
    3. Using Feature Tests to Build a Feature
    4. What to Test in an RSpec System Test
    5. Outside-in Testing
    6. Making the Capybara Test Pass
    7. Retrospective
    8. Setting Up Cucumber
    9. Writing Cucumber Features
    10. Writing Cucumber Steps
    11. Advanced Cucumber
    12. Is Cucumber Worth It?
    13. What You’ve Done
  11. 9. Testing JavaScript: Integration Testing
    1. Integration-Testing JavaScript with Capybara
    2. Let’s Talk About Drivers
    3. Making the Test Pass
    4. Webpack in Developer Mode
    5. What You’ve Done
  12. 10. Unit-Testing JavaScript
    1. Setting Up JavaScript Unit Tests
    2. Writing a Sample Test
    3. TDD in JavaScript
    4. Jasmine Matchers
    5. Testing Ajax Calls
    6. Using testdouble.js
    7. Connecting the JavaScript to the Server Code
    8. What You’ve Done
  13. 11. Testing Rails Display Elements
    1. Testing Routes
    2. Testing Helper Methods
    3. Testing Controllers and Requests
    4. Simulating Requests
    5. What to Expect in a Request Spec
    6. Older Rails Controller Tests
    7. Testing Mailers
    8. Testing Views and View Markup
    9. Using Presenters
    10. Testing Jobs and Cables
    11. What You’ve Done
  14. 12. Minitest
    1. Getting Started with Minitest
    2. Minitest Basics
    3. Running Minitest
    4. Minitest Setup
    5. Mocha
    6. System Tests and Others
    7. Minitest Helper Tests
    8. Minitest and Routing
    9. What You’ve Done
  15. 13. 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
    9. What You’ve Done
  16. 14. Testing External Services
    1. External Testing Strategy
    2. The 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
    10. What You’ve Done
  17. 15. Troubleshooting and Debugging
    1. General Principles
    2. The Humble Print Statement
    3. Git Bisect
    4. RSpec or Minitest Bisect
    5. Pry
    6. Common Rails Gotchas
    7. What You’ve Done
  18. 16. Running Tests Faster and Running Faster Tests
    1. Running Smaller Groups of Tests
    2. Running Rails in the Background
    3. Running Tests Automatically with Guard
    4. Writing Faster Tests by Bypassing Rails
    5. Recommendations for Faster Tests
    6. What You’ve Done
  19. 17. 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
    8. What You’ve Done
  20.  Bibliography

Product information

  • Title: Rails 5 Test Prescriptions
  • Author(s): Noel Rappin
  • Release date: February 2018
  • Publisher(s): Pragmatic Bookshelf
  • ISBN: 9781680502503