bobf has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to write tests for a class that reads input from a file and creates objects of another class from that data. I've already written tests for the latter class, but I'm stuck on how to write tests for the former.
For example, the Widget class looks something like this:
This class is fairly straightforward and I wrote a series of tests for it. No problem.package Widget; sub new { my ( $class, %params ) = @_; # etc } sub get_color { ... }
I also have a Container class that holds Widget objects. It looks something like this:
package Container; sub new { ... } sub load_widgets { my ( $self, $filename ) = @_; open( my $infh, '<', $filename ) or croak "..."; while( my $line = <$infh> ) { # parse data my $widget_object = Widget->new( %params ); $self->{_Container}{$id} = $widget_object; } } sub get_widgets_by_color { my ( $self, $color ) = @_; # etc }
I need to write tests for the Container class, but to do that I need to create a Container object with some test data. What is the best way to achieve this? I thought of a number of ways to approach this problem:
The idea of creating an add_widget method appeals to me. That, combined with mocking the Widget class, is the direction that I'm leaning towards. I'm still learning how to design classes and write tests, though, so please whack me with the clue stick.
Which one (or more) of these approaches sounds like the best design? Are there other alternatives that aren't listed here? I've never used Test::MockObject or Test::MockModule, so if that's the best way to go I'd appreciate some pointers.
Many thanks in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Testing methods that read input from files
by davidrw (Prior) on Jul 22, 2006 at 21:08 UTC | |
|
Re: Testing methods that read input from files
by Herkum (Parson) on Jul 23, 2006 at 13:36 UTC | |
|
Re: Testing methods that read input from files
by Anonymous Monk on Jul 25, 2006 at 08:37 UTC |