Skip to content

Instantly share code, notes, and snippets.

@DannyBen
Last active July 19, 2020 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DannyBen/6ad4ade97961856dc63efe3138046fcc to your computer and use it in GitHub Desktop.
Save DannyBen/6ad4ade97961856dc63efe3138046fcc to your computer and use it in GitHub Desktop.
Demonstrating TTY Prompt testing with rspec

Run with:

$ bundle
$ bundle exec rspec tty-prompt-test.rb
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "tty-prompt", github: 'piotrmurach/tty-prompt'
gem "rspec"
require 'tty-prompt'
require "tty/prompt/test"
class Subject
def prompt
@prompt ||= TTY::Prompt.new
end
def run
"#{name} #{destiny}"
end
def name
prompt.ask "Name:"
end
def destiny
prompt.select("Destiny:", %w(Scorpion Kano Jax))
end
end
describe Subject do
let(:prompt) { TTY::Prompt::Test.new }
before do
prompt.on :keypress do |e|
prompt.trigger :keyup if e.value == "k"
prompt.trigger :keydown if e.value == "j"
end
allow(subject).to receive(:prompt).and_return prompt
end
it "works" do
prompt.input << "Danny" << "\n" << "j" << "\n"
prompt.input.rewind
expect(subject.run).to eq "Danny Kano"
end
it "works for sure" do
prompt.input << "Danny" << "\n" << "j" << "j" << "\n"
prompt.input.rewind
expect(subject.run).to eq "Danny Jax"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment