Testing User Input With Rspec


2 min read

Last week, I mentioned that I would like to focus on testing input with RSpec. This week’s task provided the perfect opportunity to do so.

The task: Contact manager

The aim was to create a command line program to store, search and view the contacts, with the following features:

  • Core: User should be able to enter a contact (name, email, phone number)
  • Core: User should be able to see a list of all of my contacts in alphabetical order
  • Core: User should be able to search for a contact by name and view their details
  • Stretch goal: Save the contacts to a file
  • Stretch goal: Load the contacts from that file when restarting the program

Testing user input

With the first feature (user should be able to enter a contact), I imagined the user would need to provide input.

I stumbled across a post on Stack Overflow — using RSpec to test user input with gets. The key part here was using a stub for gets.

Multiple inputs also needed to be simulated. I came across a post on Stack Overflow related to this.

Using the last post as a guide, I created a new test. That didn’t work as expected, however this showed I could have multiple inputs. Using the first example from Slack Overflow, I combined this with the second to come up with the following test:


Things I've learned

  • Testing input with RSpec and multiple inputs

Things I've struggled with

  • 🤷🏽‍♀️

Things I want to focus on

  • Uncovering what else can be tested with RSpec
Previous post:
What Is Dependency Injection?
Next post:
An Introduction To JSON

Discussion