RSpec Essentials

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

RSpec Essentials

This ebook is included in a Mapt subscription
Mani Tadayon

Develop testable, modular, and maintainable Ruby software for the real world using RSpec
$0.00
$16.00
$39.99
$29.99p/m after trial
RRP $31.99
RRP $39.99
Subscription
eBook
Print + eBook
Start 30 Day Trial
Subscribe and access every Packt eBook & Video.
 
  • 4,000+ eBooks & Videos
  • 40+ New titles a month
  • 1 Free eBook/Video to keep every month
Start Free Trial
 
Preview in Mapt

Book Details

ISBN 139781784395902
Paperback222 pages

Book Description

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.

Table of Contents

Chapter 1: Exploring Testability from Unit Tests to Behavior-Driven Development
The promise of testing
Testability
Technical assumptions
Running our first spec
Understanding the unit test
Writing specs with RSpec
Test-driven development
Behavior-driven development
Summary
Chapter 2: Specifying Behavior with Examples and Matchers
Structure of a spec file
Using let and context
Matchers
Testing for errors
Summary
Chapter 3: Taking Control of State with Doubles and Hooks
Why mock?
Mocks, stubs, and doubles
Using hooks
Controlling when hooks are executed
Advanced state control with hooks and mocks
Summary
Chapter 4: Setting Up and Cleaning Up
Configuring RSpec with spec_helper.rb
Initialization and configuration of resources
Summary
Chapter 5: Simulating External Services
The importance of external web services
Mock HTTP responses with custom helpers
Using VCR for mock requests
Summary
Chapter 6: Driving a Web Browser with Capybara
Getting started with Capybara and Selenium
Integrating Capybara with RSpec
Why Capybara?
Black-box JavaScript tests with a real browser
Summary
Chapter 7: Building an App from the Outside In with Behavior-Driven Development
Exploring BDD
MMFs by example
Using TodoMVC
Specifying the MMF
Configuring RSpec to run a feature file
The BDD process begins
Building web apps with Rack
Serving static files using Rack middleware
Summary
Chapter 8: Tackling the Challenges of End-to-end Testing
Step 1 – building the view
Step 2 – defining the API
Step 3 – finishing the view
The pros and cons of BDD
Simple Rack authentication middleware
JSON Web Token
Summary
Chapter 9: Configurability
Configuration and testability
File-based configuration management
A better configuration manager
Summary
Chapter 10: Odds and Ends
Reducing duplication with shared example groups
Mocking time
Detecting false negatives and false positives
Testing mixins with dummy containers
Summary

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

Authors

Table of Contents

Chapter 1: Exploring Testability from Unit Tests to Behavior-Driven Development
The promise of testing
Testability
Technical assumptions
Running our first spec
Understanding the unit test
Writing specs with RSpec
Test-driven development
Behavior-driven development
Summary
Chapter 2: Specifying Behavior with Examples and Matchers
Structure of a spec file
Using let and context
Matchers
Testing for errors
Summary
Chapter 3: Taking Control of State with Doubles and Hooks
Why mock?
Mocks, stubs, and doubles
Using hooks
Controlling when hooks are executed
Advanced state control with hooks and mocks
Summary
Chapter 4: Setting Up and Cleaning Up
Configuring RSpec with spec_helper.rb
Initialization and configuration of resources
Summary
Chapter 5: Simulating External Services
The importance of external web services
Mock HTTP responses with custom helpers
Using VCR for mock requests
Summary
Chapter 6: Driving a Web Browser with Capybara
Getting started with Capybara and Selenium
Integrating Capybara with RSpec
Why Capybara?
Black-box JavaScript tests with a real browser
Summary
Chapter 7: Building an App from the Outside In with Behavior-Driven Development
Exploring BDD
MMFs by example
Using TodoMVC
Specifying the MMF
Configuring RSpec to run a feature file
The BDD process begins
Building web apps with Rack
Serving static files using Rack middleware
Summary
Chapter 8: Tackling the Challenges of End-to-end Testing
Step 1 – building the view
Step 2 – defining the API
Step 3 – finishing the view
The pros and cons of BDD
Simple Rack authentication middleware
JSON Web Token
Summary
Chapter 9: Configurability
Configuration and testability
File-based configuration management
A better configuration manager
Summary
Chapter 10: Odds and Ends
Reducing duplication with shared example groups
Mocking time
Detecting false negatives and false positives
Testing mixins with dummy containers
Summary

Book Details

ISBN 139781784395902
Paperback222 pages
Read More

Read More Reviews