Let's look again at the AddressValidator
module we introduced in Chapter 1, Exploring Testability from Unit Tests to Behavior-Driven Development, so we can understand its structure better. We'll also use some basic RSpec features to improve the tests. Let's look at the spec code:
require 'rspec' require_relative 'address_validator' describe AddressValidator do it "returns false for incomplete address" do address = { street: "123 Any Street", city: "Anytown" } expect( AddressValidator.valid?(address) ).to eq(false) end it "missing_parts returns an array of missing required parts" do address = { street: "123 Any Street", city: "Anytown" } expect( AddressValidator.missing_parts(address) ).to eq([:region, :postal_code, :country]) end end
We defined a local variable address
in each example. This is fine for simple, one-off values. We could get the same functionality shared across multiple tests with a local function defined...