# Testing a class method that also is called in initialize

**URL:** <https://www.ruby-forum.com/t/testing-a-class-method-that-also-is-called-in-initialize/184703>\
**Category:** Ruby\
**Created:** [March 7, 2010, 2:17am UTC](https://www.ruby-forum.com/t/testing-a-class-method-that-also-is-called-in-initialize/184703 "2010-03-07T02:17:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [March 7, 2010, 2:17am UTC](https://www.ruby-forum.com/t/testing-a-class-method-that-also-is-called-in-initialize/184703/1 "2010-03-07T02:17:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [March 7, 2010, 10:34am UTC](https://www.ruby-forum.com/t/testing-a-class-method-that-also-is-called-in-initialize/184703/2 "2010-03-07T10:34:08Z")

</div>

On Sun, Mar 7, 2010 at 2:17 AM, Adam A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> ....
> 
> ```
> 
> 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.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

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 = 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

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [March 7, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/testing-a-class-method-that-also-is-called-in-initialize/184703/3 "2010-03-07T18:00:05Z")

</div>

On 3/6/10, Adam A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> ....
> 
> ```
> 
> 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.
