Skip to content

Instantly share code, notes, and snippets.

@martinos
Last active August 29, 2015 14:17
Show Gist options
  • Save martinos/a26769ac33dea0a4b668 to your computer and use it in GitHub Desktop.
Save martinos/a26769ac33dea0a4b668 to your computer and use it in GitHub Desktop.
How to test a cli app
require 'minitest/autorun'
require 'expect'
Thread::abort_on_exception = true
class EchoServer
def initialize(stdin: , stdout:)
@stdin = stdin
@stdout = stdout
end
def run
while (in_str = @stdin.gets) && in_str !~ /quit/ do
@stdout.puts(in_str)
end
end
end
describe EchoServer do
before do
stdin_reader, @stdin_writer = IO.pipe
@stdout_reader, stdout_writer = IO.pipe
@echo = EchoServer.new(stdin: stdin_reader, stdout: stdout_writer)
@thread = Thread.new { @echo.run }
end
it 'should echo the stdin to stdout' do
@stdin_writer.puts("martin")
@stdout_reader.expect(/martin/, 1).wont_be_nil
end
it 'should exit on "quit"' do
@stdin_writer.puts("quit")
@thread.join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment