Skip to content

Commit a03af24

Browse files
committed
Set provided test input via a function
1 parent 144bcb2 commit a03af24

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

spec/support/highline_test_helpers.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
module HighLineTestHelpers
66
def prepare_highline
7-
@input = instance_double(IO, eof?: false, gets: "q\n")
7+
@input = instance_double(IO, eof?: false, gets: nil)
88
@output = StringIO.new
99
Imap::Backup::Setup.highline = HighLine.new(@input, @output)
1010
[@input, @output]
1111
end
12+
13+
def set_highline_input(*lines)
14+
allow(@input).to receive(:gets).and_return(*lines.map { |line| "#{line}\n" })
15+
end
1216
end

spec/unit/setup/folder_chooser_spec.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Imap::Backup
2727
Translator.new.setup
2828
allow(Kernel).to receive(:system)
2929
allow(Logger.logger).to receive(:warn)
30+
set_highline_input("q\n")
3031
end
3132

3233
describe "display" do
@@ -51,7 +52,11 @@ module Imap::Backup
5152
end
5253

5354
describe "display" do
54-
before { subject.run }
55+
before do
56+
set_highline_input("q\n")
57+
58+
subject.run
59+
end
5560

5661
it "shows folders which are being backed up" do
5762
expect(output.string).to include("+ my_folder")
@@ -64,7 +69,7 @@ module Imap::Backup
6469

6570
context "when adding folders" do
6671
before do
67-
allow(input).to receive(:gets).and_return("2\n", "q\n")
72+
set_highline_input("2\n", "q\n")
6873

6974
subject.run
7075
end
@@ -77,7 +82,7 @@ module Imap::Backup
7782

7883
context "when removing folders" do
7984
before do
80-
allow(input).to receive(:gets).and_return("1\n", "q\n")
85+
set_highline_input("1\n", "q\n")
8186

8287
subject.run
8388
end

spec/unit/setup/global_options/download_strategy_chooser_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module Imap::Backup
2222
Translator.new.setup
2323
allow(Kernel).to receive(:system)
2424
allow(Kernel).to receive(:puts)
25+
set_highline_input("q\n")
2526
end
2627

2728
it "clears the screen" do
@@ -37,15 +38,15 @@ module Imap::Backup
3738
end
3839

3940
it "accepts choices" do
40-
allow(input).to receive(:gets).and_return("2\n", "q\n")
41+
set_highline_input("2\n", "q\n")
4142

4243
subject.run
4344

4445
expect(config).to have_received(:download_strategy=).with("delay_metadata")
4546
end
4647

4748
it "shows help" do
48-
allow(input).to receive(:gets).and_return("help\n", "q\n")
49+
set_highline_input("help\n", "q\n")
4950

5051
subject.run
5152

@@ -62,7 +63,7 @@ module Imap::Backup
6263
end
6364

6465
it "marks the entry as current" do
65-
allow(input).to receive(:gets) { "q\n" }
66+
set_highline_input("q\n")
6667

6768
subject.run
6869

spec/unit/setup/global_options_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Imap::Backup
2727
allow(Kernel).to receive(:system)
2828
allow(Setup::GlobalOptions::DownloadStrategyChooser).
2929
to receive(:new) { download_strategy_chooser }
30+
set_highline_input("q\n")
3031
end
3132

3233
it "clears the screen" do
@@ -58,7 +59,7 @@ module Imap::Backup
5859
end
5960

6061
it "accepts choices" do
61-
allow(input).to receive(:gets).and_return("1\n", "q\n")
62+
set_highline_input("1\n", "q\n")
6263

6364
subject.run
6465

spec/unit/setup_spec.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ module Imap::Backup
5353
before do
5454
Translator.new.setup
5555
allow(Logger).to receive(:setup_logging)
56-
allow(input).to receive(:eof?) { false }
57-
allow(input).to receive(:gets) { "q\n" }
5856
allow(Kernel).to receive(:system)
57+
set_highline_input("q\n")
5958
end
6059

6160
describe "main menu" do
@@ -73,7 +72,7 @@ module Imap::Backup
7372
let(:config_modified) { true }
7473

7574
before do
76-
allow(input).to receive(:gets) { "exit\n" }
75+
set_highline_input("exit\n")
7776
subject.run
7877
end
7978

@@ -129,7 +128,7 @@ module Imap::Backup
129128
let(:global_options) { instance_double(Setup::GlobalOptions, run: nil) }
130129

131130
before do
132-
allow(input).to receive(:gets).and_return("3\n", "q\n")
131+
set_highline_input("3\n", "q\n")
133132
allow(Setup::GlobalOptions).
134133
to receive(:new).with(config: config) { global_options }
135134
end
@@ -148,7 +147,7 @@ module Imap::Backup
148147
let(:config_modified) { true }
149148

150149
before do
151-
allow(input).to receive(:gets).and_return("1\n", "exit\n")
150+
set_highline_input("1\n", "exit\n")
152151
allow(Setup::Asker).to receive(:email).
153152
with(no_args) { "new@example.com" }
154153
allow(Setup::Account).to receive(:new).
@@ -179,8 +178,8 @@ module Imap::Backup
179178
end
180179

181180
before do
182-
allow(input).to receive(:gets).and_return("add\n", "exit\n")
183181
allow(config.accounts).to receive(:<<)
182+
set_highline_input("add\n", "exit\n")
184183
allow(Setup::Asker).to receive(:email).
185184
with(no_args) { added_email }
186185
allow(Setup::Account).to receive(:new).
@@ -244,7 +243,7 @@ module Imap::Backup
244243
let(:config_modified) { true }
245244

246245
before do
247-
allow(input).to receive(:gets) { "save\n" }
246+
set_highline_input("save\n")
248247
end
249248

250249
it "exits" do
@@ -263,7 +262,7 @@ module Imap::Backup
263262
let(:config_modified) { true }
264263

265264
before do
266-
allow(input).to receive(:gets) { "exit\n" }
265+
set_highline_input("exit\n")
267266
end
268267

269268
it "exits" do

0 commit comments

Comments
 (0)