testing a class method that also is called in initialize

A

Adam Akhtar

Hi im still learning testing and bit confused about testing the
parse_file method below


class Data
def initialize a_file
@records = parse_file(a_file)
end

def parse_file(a_file)
....
end
end

To test parse_file i must first create a new data object i.e.

sample_data = Data.new("survey")
then in my test

assert_equal blah, sample_data.parse_file("sample.txt")

but in the test when i create a new data object the the method will be
invoked via initialize before ive even tested it which doesnt sit right
with me.

Should i be mocking this? Or is there a simpler solution to this.
 
R

Robert Dober

Hi im still learning testing and bit confused about testing the
parse_file method below


class Data
=A0def initialize a_file
=A0 =A0 @records =3D parse_file(a_file)
=A0end

=A0 def parse_file(a_file)
=A0 =A0 =A0....
=A0 end
end

To test parse_file i must first create a new data object i.e.

sample_data =3D Data.new("survey")
then in my test

assert_equal blah, sample_data.parse_file("sample.txt")

but in the test when i create a new data object the the method =A0will be
invoked via initialize before ive even tested it which doesnt sit right
with me.

Should i be mocking this? Or is there a simpler solution to this.

Hmm seems you want to test something you did not implement.
A first but radical measure would be to do some [BT]DD. That means
that you have to throw away your code because you simply do not hava
any right to write code that does not make any tests pass... Then
write your tests (I prefer specs or behaviors) and then implement
them. Now your issue above should be a non issue (I guess, but if it
is please post again).

A second idea is to do some very nasty setup like

object =3D Data.allocate
assert_equal blah, object.parse

OMG this code is sooo fragile, but it might save your day.

And last but not least mocking isa powerful technique, I do not know a
lot about it :(. Here it's use might hide a design flaw though IMHO.

HTH
Robert
--=20
Learning without thought is labor lost; thought without learning is perilou=
s.=94
--- Confucius
 
C

Caleb Clausen

Hi im still learning testing and bit confused about testing the
parse_file method below


class Data
def initialize a_file
@records = parse_file(a_file)
end

def parse_file(a_file)
....
end
end

To test parse_file i must first create a new data object i.e.

sample_data = Data.new("survey")
then in my test

assert_equal blah, sample_data.parse_file("sample.txt")

but in the test when i create a new data object the the method will be
invoked via initialize before ive even tested it which doesnt sit right
with me.

What harm is going to come of letting parse_file run before the
assertion which tests it? I don't understand your concern.

IMNSHO, the assignment to @records should occur inside of parse_file,
OR parse_file should be private.
Should i be mocking this? Or is there a simpler solution to this.

Avoid mocks unless they're really necessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top