You are previewing RSpec Essentials.
O'Reilly logo
RSpec Essentials

Book Description

Develop testable, modular, and maintainable Ruby software for the real world using RSpec

About This Book

  • Explore the concept of testability and how to implement tests that deliver the most value

  • Maximize the quality of your Ruby code through a wide variety of tests

  • Master the real-world tradeoffs of testing through detailed examples supported by in-depth discussion

  • Who This Book Is For

    This book is aimed at the software engineer who wants to make their code more reliable and their development process easier. It is also aimed at test engineers who need to automate the testing of complex systems. Knowledge of Ruby is helpful, but even someone new to the language should find it easy to follow the code and tests.

    What You Will Learn

  • Identify a unit of software for the purposes of testing

  • Manage test states with hooks, fixtures, and mocks

  • Handle external web services in tests using various techniques

  • Configure RSpec flexibly and cleanly using support code and environment variables

  • Interact with rich web apps in tests using Capybara

  • Build the right feature with behavior-driven development

  • Customize matchers and failure messages

  • Verify correct development and production environments

  • In Detail

    This book will teach you how to use RSpec to write high-value tests for real-world code. We start with the key concepts of the unit and testability, followed by hands-on exploration of key features. From the beginning, we learn how to integrate tests into the overall development process to help create high-quality code, avoiding the dangers of testing for its own sake.

    We build up sample applications and their corresponding tests step by step, from simple beginnings to more sophisticated versions that include databases and external web services. We devote three chapters to web applications with rich JavaScript user interfaces, building one from the ground up using behavior-driven development (BDD) and test-driven development (TDD).

    The code examples are detailed enough to be realistic while simple enough to be easily understood. Testing concepts, development methodologies, and engineering tradeoffs are discussed in detail as they arise. This approach is designed to foster the reader’s ability to make well-informed decisions on their own.

    Style and approach

    This comprehensive tutorial is packed with real-world examples of testing with RSpec. The most important features of RSpec are introduced in the early chapters and are used in examples of growing complexity in the following chapters. Concepts and methodologies are discussed in detail.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. RSpec Essentials
      1. Table of Contents
      2. RSpec Essentials
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. eBooks, discount offers, and more
          1. Why subscribe?
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      8. 1. Exploring Testability from Unit Tests to Behavior-Driven Development
        1. The promise of testing
        2. Testability
        3. Technical assumptions
        4. Running our first spec
        5. Understanding the unit test
        6. Writing specs with RSpec
        7. Test-driven development
        8. Behavior-driven development
        9. Summary
      9. 2. Specifying Behavior with Examples and Matchers
        1. Structure of a spec file
        2. Using let and context
        3. Matchers
          1. Built-in matchers
          2. Custom matchers
          3. Enhanced context in matcher output
          4. Creating a good custom error message
          5. Improving application code
        4. Testing for errors
        5. Summary
      10. 3. Taking Control of State with Doubles and Hooks
        1. Why mock?
        2. Mocks, stubs, and doubles
        3. Using hooks
        4. Controlling when hooks are executed
        5. Advanced state control with hooks and mocks
        6. Summary
      11. 4. Setting Up and Cleaning Up
        1. Configuring RSpec with spec_helper.rb
        2. Initialization and configuration of resources
          1. A case study of test simplicity with an external service
          2. A case study of production faithfulness with a test resource instance
        3. Summary
      12. 5. Simulating External Services
        1. The importance of external web services
        2. Mock HTTP responses with custom helpers
          1. Using real-world requests for mock responses
          2. Using WebMock for mock HTTP requests
        3. Using VCR for mock requests
        4. Summary
      13. 6. Driving a Web Browser with Capybara
        1. Getting started with Capybara and Selenium
        2. Integrating Capybara with RSpec
        3. Why Capybara?
        4. Black-box JavaScript tests with a real browser
        5. Summary
      14. 7. Building an App from the Outside In with Behavior-Driven Development
        1. Exploring BDD
        2. MMFs by example
        3. Using TodoMVC
        4. Specifying the MMF
        5. Configuring RSpec to run a feature file
        6. The BDD process begins
        7. Building web apps with Rack
        8. Serving static files using Rack middleware
        9. Summary
      15. 8. Tackling the Challenges of End-to-end Testing
        1. Step 1 – building the view
        2. Step 2 – defining the API
        3. Step 3 – finishing the view
        4. The pros and cons of BDD
        5. Simple Rack authentication middleware
        6. JSON Web Token
        7. Summary
      16. 9. Configurability
        1. Configuration and testability
          1. What is so important about configuration?
          2. What does all this have to do with testing?
        2. File-based configuration management
        3. A better configuration manager
        4. Summary
      17. 10. Odds and Ends
        1. Reducing duplication with shared example groups
        2. Mocking time
        3. Detecting false negatives and false positives
        4. Testing mixins with dummy containers
        5. Summary
      18. Index