Input / Output Tests
What is your first name?
Jane
What is your last name?
Doe
Hello, Jane Doe!Grading Tests:
require 'rspec'
require 'stringio'
student_code = File.read("/usercode/example.rb")
new_student_code = student_code.gsub(" gets", " $stdin.gets")
File.open("/usercode/example.rb", "w") do |f|
f.write(new_student_code)
end
RSpec.describe "CodingRoomsUnitTests" do
describe 'Giving code Jane and Doe as inputs' do
it "output should equal 'Hello, Jane Doe!'" do
input = StringIO.new()
output = StringIO.new()
input.puts "Jane"
input.puts "Doe"
input.rewind
$stdin = input
$stdout = output
load '/usercode/example.rb'
expect(output.string).to include("Hello, Jane Doe!")
$stdin = STDIN
$stdout = STDOUT
end
end
describe 'Giving code Joe and Mazzone as inputs' do
it "output should equal 'Hello, Joe Mazzone!'" do
input = StringIO.new()
output = StringIO.new()
input.puts "Joe"
input.puts "Mazzone"
input.rewind
$stdin = input
$stdout = output
load '/usercode/example.rb'
expect(output.string).to include("Hello, Joe Mazzone!")
$stdin = STDIN
$stdout = STDOUT
end
end
endSample Solution:
Last updated
Was this helpful?