Print Function Tests
This is an example of how you can test the output stream of a student defined function.
Ruby makes this super easy with the output matcher.
expect { print 'foo' }.to output('foo').to_stdout
This test makes sure the student's function named prints_name() that takes a name as a parameter and prints "Hello, {name provided}!" to standard output. It assumes the student's file name is example.rb. Replace the require with whatever your student's file name is.

Grading Tests:

1
require 'rspec'
2
require 'stringio'
3
require '/usercode/example'
4
​
5
RSpec.describe "CodingRoomsUnitTests" do
6
​
7
describe "Run prints_name() with 'Jane'" do
8
it "expects output to be 'Hello, Jane!'" do
9
expect{prints_name("Jane")}.to output("Hello, Jane!\n").to_stdout
10
end
11
end
12
​
13
describe "Run prints_name() with 'Fred'" do
14
it "expects output to be 'Hello, Fred!'" do
15
expect{prints_name("Fred")}.to output("Hello, Fred!\n").to_stdout
16
end
17
end
18
​
19
end
20
​
Copied!

Sample Solution:

example.rb
1
def prints_name(name)
2
puts 'Hello, ' + name + "!"
3
end
4
​
5
prints_name("Jane")
Copied!
Export as PDF
Copy link
Contents