testing a class method that also is called in initialize

Discussion in 'Ruby' started by Adam Akhtar, Mar 7, 2010.

  1. Adam Akhtar

    Adam Akhtar Guest

    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.
    --
    Posted via http://www.ruby-forum.com/.
    Adam Akhtar, Mar 7, 2010
    #1
    1. Advertising

  2. Adam Akhtar

    Robert Dober Guest

    On Sun, Mar 7, 2010 at 2:17 AM, Adam Akhtar <> wrote=
    :
    > 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.
    > --
    > Posted via 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 =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
    Robert Dober, Mar 7, 2010
    #2
    1. Advertising

  3. On 3/6/10, Adam Akhtar <> wrote:
    > 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.
    Caleb Clausen, Mar 7, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edvard Majakari
    Replies:
    4
    Views:
    669
    Edvard Majakari
    Feb 25, 2005
  2. S_K
    Replies:
    6
    Views:
    1,156
    Robert Dunlop
    Nov 8, 2007
  3. Replies:
    10
    Views:
    175
    matt neuburg
    Oct 27, 2006
  4. francisco lopez
    Replies:
    1
    Views:
    157
    Michael Winter
    Jan 4, 2005
  5. Marc Aymerich
    Replies:
    11
    Views:
    217
    Steven D'Aprano
    Nov 23, 2012
Loading...

Share This Page