Book Image

Testing Ruby with RSpec: The Complete Guide [Video]

By : Boris Paskhaver
Book Image

Testing Ruby with RSpec: The Complete Guide [Video]

By: Boris Paskhaver

Overview of this book

This course offers a comprehensive overview of the RSpec testing library for the Ruby programming library. RSpec is the most popular Ruby Gem of all time, with over 300 million downloads to date. If you are new to the topic, testing is the practice of writing code that confirms that other code works as expected. Tests control regressions, which are changes to the code that breaks the program. The benefits of testing extend beyond the codebase. Adopting a test-driven approach will also make you a better developer. Tests force you to think critically about a program and its features: classes, objects, methods, and more. This course begins with the essentials and proceeds to more complex topics, including installation, project initialization, Test-Driven Development (TDD), let variables, before and after hooks, subjects, shared examples, shared context, built-in matches, mocks and doubles, instance doubles, and class doubles. At the completion of this course, you will have acquired hands-on Ruby testing experience with this awesome RSpec library. All Resources for this course are available at https://github.com/packtpublishing/testing-ruby-with-rspec-the-complete-guide
Table of Contents (5 chapters)
Chapter 1
Introduction
Content Locked
Section 13
Reducing Duplication - Before Hooks and Instance Variables
Multiple examples in a test suite often rely on the same piece of data or a common object. Duplicating the code to create or access that object in each example creates a lot of duplication. To DRY up the code, this lesson introduces before hooks and instance variables. A hook is a piece of code that runs automatically at a specific time during testing. By default, the before hook executes a block of code before each example. Within the block, we assign values to instance variables to preserve values once the block execution ends. These instance variables are then available to be used within any examples defined below.