Using RSpec to Test Console Application Interaction


Staff member
I have several console applications that have a read, evaluate, print, loop (REPL) using readline that I would like to create tests for using RSpec if possible. I've read about mock classes, but I don't see how they would work. Is it only possible to replace a real input class to test the rest of the classes? I want to do an integration test of the whole app. For one of my web services, I have an rspec script that uses curl to send requests to test the socket interface. I wanted to do something similar for the console interface. Am I just not understanding the concept of mock classes?


Based on the answer @Dave Schweisguth gave, I tried this:

require 'rspec'
require 'pty'

describe "anthematic" do
  before(:all) do
    @output, @input = PTY.spawn('bin/anthematic')

  it "turns on a zone" do
    @output.readpartial 1024 # read past the prompt
    @input.puts "on 1"
    expect(@output.readline.chomp).to eq("on 1")
    expect(@output.readline.chomp).to eq("turn on zone 1")

I moved the spawn out so I can add other tests. I'm new to rspec so that may not be the cleanest solution.

While researching PTY, I found <a href="" rel="nofollow">this article</a> on sub-processes that covers popen, pty, and others.