Skip to content

Instantly share code, notes, and snippets.

@DanielVartanov
Created July 21, 2020 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielVartanov/8eac7a04fb77322f9422cdf1ed9d67f4 to your computer and use it in GitHub Desktop.
Save DanielVartanov/8eac7a04fb77322f9422cdf1ed9d67f4 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
module TTY
module StringIOExtensions
def wait_readable(*)
true
end
def ioctl(*)
80
end
end
def Prompt.test_mode!
mod = Module.new do
def initialize(**options)
input = StringIO.new
input.extend(StringIOExtensions)
output = StringIO.new
super(**options.merge(input: input, output: output))
end
def input=(string)
input << string
input.rewind
end
def output
@output.string
end
end
prepend mod
end
end
# in spec_helper.rb
TTY::Prompt.test_mode!
RSpec.describe "console app" do
let(:prompt) { TTY::Prompt.new } # No TTY::Prompt::Test
subject do
name = prompt.ask("Name?")
if prompt.yes?("Greetings?")
prompt.say "Hello, #{name}!"
end
end
specify do
prompt.input = "Danny\nY\n"
subject
expect(prompt.output).to end_with("Hello, Danny!\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment