Your method get_symbols_from_file
is never called in the test. You're just testing that StringIO#readlines
works, i.e.:
StringIO.new("YHOO,141414").readlines == ["YHOO,141414"] #=> true
If you want to use a StringIO
instance as a placeholder for your file, you have to change your method to take a File
instance rather than a file name:
def get_symbols_from_file(file)
file.readlines(',')
end
Both, File
and StringIO
instances respond to readlines
, so the above implementation can handle both:
def test_get_symbols_from_file
s = StringIO.new("YHOO,141414")
assert_equal(["YHOO,141414"], get_symbols_from_file(s))
end
This test however fails: readlines
includes the line separator, so it returns an array with two elements "YHOO,"
(note the comma) and "141414"
. You are expecting an array with one element "YHOO,141414"
.
Maybe you're looking for something like this:
def test_get_symbols_from_file
s = StringIO.new("YHOO,141414")
assert_equal(["YHOO", "141414"], get_symbols_from_file(s))
end
def get_symbols_from_file(file)
file.read.split(',')
end
If you really want to use IO::readlines
you could create a Tempfile
:
require 'tempfile'
def test_get_symbols_from_file
Tempfile.open("foo") { |f|
f.write "YHOO,141414"
f.close
assert_equal(["YHOO", "141414"], get_symbols_from_file(f.path))
}
end