forked from appdev-projects/array-chapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_count_2_spec.rb
More file actions
25 lines (21 loc) · 1.14 KB
/
array_count_2_spec.rb
File metadata and controls
25 lines (21 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
describe "array_count.rb" do
it "should output '9' given the input '9 12 8 25 16 78 64 0 27'" , points: 1 do
# Un-require array_count.rb
array_count = $".select{|r| r.include? 'array_count.rb'}
$".delete(array_count.first)
allow_any_instance_of(Object).to receive(:gets).and_return("9 12 8 25 16 78 64 0 27\n")
# expect { require_relative '../../array_count' }.to output(/"?Enter at least 2 numbers, separated by spaces:"?\n9\n/).to_stdout
output = with_captured_stdout { require_relative('../../array_count')}
output = "empty" if output.empty?
expect(output.match?(/"?Enter at least 2 numbers, separated by spaces:"?\n9\n/i)).to be(true),
"Expected output to be 'Enter at least 2 numbers, separated by spaces:\\n9\n', but was #{output}."
end
end
def with_captured_stdout
original_stdout = $stdout # capture previous value of $stdout
$stdout = StringIO.new # assign a string buffer to $stdout
yield # perform the body of the user code
$stdout.string # return the contents of the string buffer
ensure
$stdout = original_stdout # restore $stdout to its previous value
end