Rails 5 Test Prescriptions
Build a Healthy Codebase
by: Noel Rappin
Published | 2018-02-14 |
---|---|
Internal code | nrtest3 |
Print status | In Print |
Pages | 404 |
User level | Intermediate |
Keywords | ruby, rails, testing, TDD, test driven, test recipes, recipes, Cucumber, Shoulda, Machinist, Mocha, Rcov |
Related titles | Effective Testing With RSpec |
ISBN | 9781680502503 |
Other ISBN |
Channel epub: 9781680505573 Channel PDF: 9781680505580 Kindle: 9781680505559 Safari: 9781680505566 Kindle: 9781680505559 |
BISACs | COM051410 COMPUTERS / Programming Languages / RubyCOM051330 COMPUTERS / Software Development & Engineering / Quality Assurance & TestingCOM051330 COMPUTERS / Software Development & Engineering / Quality Assurance & Testing |
Highlight
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.2, Minitest 5, and RSpec 3.7, as well as popular testing libraries such as factory_bot and Cucumber. Updates include Rails 5.2 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.
Description
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_bot. 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.2 and RSpec 3.7 and contains full coverage of new Rails features, including system tests and the Webpack-based JavaScript setup.
Contents and Extracts
- Acknowledgments
- Preface
- Who You Are
- What’s in This Book
- What You’ll Need
- A Word About Tools, Best Practices, and Teaching TDD
- Changes in This Edition
- Online Resources
- A Test-Driven Fable
- Testing First Drives Design
- What Is TDD Good For?
- When TDD Needs Some Help
- What You’ve Done
- Test-Driven Development Basics
- Infrastructure
- The Requirements
- Installing RSpec
- Where to Start? <b>excerpt</b>
- Running the Test
- Making the Test Pass
- The Second Test
- Back on Task
- Adding Some Math
- The First Date
- Using the Time Data
- What You’ve Done
- Test-Driven Rails
- Let’s Write Some Rails
- The Days Are Action-Packed
- Who Controls the Controller?
- A Test with a View
- Testing for Failure
- What You’ve Done
- What Makes Great Tests
- The Big One
- Cost and Value
- SWIFT: The Five Qualities of Valuable Tests
- What You’ve Done
- Testing Models <b>excerpt</b>
- What Can You Do in a Model Test?
- What Should You Test in a Model Test?
- Okay, Funny Man, What Makes a Good Set of Model Tests?
- Refactoring Models
- A Note on Assertions per Test
- Testing What Rails Gives You
- Testing ActiveRecord Finders
- Testing Shared Modules and ActiveSupport Concerns
- Writing Your Own RSpec Matchers
- What You’ve Done
- Adding Data to Tests
- What’s the Problem?
- Fixtures
- Factories
- Dates and Times
- What You’ve Done
- Using Test Doubles as Mocks and Stubs
- Test Doubles Defined
- Creating Stubs
- Mock Expectations
- Using Mocks to Simulate Database Failure
- Using Mocks to Specify Behavior
- More Expectation Annotations
- Mock Tips
- What You’ve Done
- Integration Testing with Capybara and Cucumber
- A Field Guide to Integration and System Tests
- Setting Up Capybara
- Using Feature Tests to Build a Feature
- What to Test in an RSpec System Test
- Outside-in Testing
- Making the Capybara Test Pass
- Retrospective
- Setting Up Cucumber
- Writing Cucumber Features
- Writing Cucumber Steps
- Advanced Cucumber
- Is Cucumber Worth It?
- What You’ve Done
- Testing JavaScript: Integration Testing
- Integration-Testing JavaScript with Capybara
- Let’s Talk About Drivers
- Making the Test Pass
- Webpack in Developer Mode
- What You’ve Done
- Unit-Testing JavaScript
- Setting Up JavaScript Unit Tests
- Writing a Sample Test
- TDD in JavaScript
- Jasmine Matchers
- Testing Ajax Calls
- Using testdouble.js
- Connecting the JavaScript to the Server Code
- What You’ve Done
- Testing Rails Display Elements
- Testing Routes
- Testing Helper Methods <b>excerpt</b>
- Testing Controllers and Requests
- Simulating Requests
- What to Expect in a Request Spec
- Older Rails Controller Tests
- Testing Mailers
- Testing Views and View Markup
- Using Presenters
- Testing Jobs and Cables
- What You’ve Done
- Minitest
- Getting Started with Minitest
- Minitest Basics
- Running Minitest
- Minitest Setup
- Mocha
- System Tests and Others
- Minitest Helper Tests
- Minitest and Routing
- What You’ve Done
- Testing for Security
- User Authentication and Authorization
- Adding Users and Roles
- Restricting Access
- More Access-Control Testing
- Using Roles
- Protection Against Form Modification
- Mass Assignment Testing
- Other Security Resources
- What You’ve Done
- Testing External Services
- External Testing Strategy
- The Service Integration Test
- Introducing VCR
- Client Unit Tests
- Why an Adapter?
- Adapter Tests
- Testing for Error Cases
- Smoke Tests and VCR Options
- The World Is a Service
- What You’ve Done
- Troubleshooting and Debugging
- General Principles
- The Humble Print Statement
- Git Bisect
- RSpec or Minitest Bisect
- Pry
- Common Rails Gotchas
- What You’ve Done
- Running Tests Faster and Running Faster Tests
- Running Smaller Groups of Tests
- Running Rails in the Background
- Running Tests Automatically with Guard
- Writing Faster Tests by Bypassing Rails
- Recommendations for Faster Tests
- What You’ve Done
- Testing Legacy Code
- What’s a Legacy?
- Set Expectations
- Getting Started with Legacy Code
- Test-Driven Exploration
- Dependency Removal
- Find the Seam
- Don’t Look Back
- What You’ve Done