Created
July 21, 2020 20:20
-
-
Save DanielVartanov/8eac7a04fb77322f9422cdf1ed9d67f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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