J
jmv
Hello,
I have a problem creating mock objects for the unit tests. I'm writing
a unit tests for the rather old proprietary OO application that has all
sort of code smells.
The code I want to test looks like this:
=cut=
use Corp::System; # exports runCommand()
use Logcheck;
sub new { ... }
sub createUser {
...
my $exitval = runCommand("useradd ...");
...
Logcheck::createLogDir();
...
}
=cut=
I want to override the runCommand method from Corp::System module. I've
found only one way to do it. In my unit test module I write:
=cut=
BEGIN {
use Corp::System;
package Corp::System;
sub runCommand { push @COMMANDS, shift; return 0; }
1;
}
=cut=
But this method is very boring, unelegant and error-prone, because sub
createUser, which I'm testing, uses subroutines from a lot of modules
and there're numerous dependencies between them.
Is there more elegant and flexible method for testing such classes
without refactoring them? In any case, tests should exist before
refactoring.
Eugene
I have a problem creating mock objects for the unit tests. I'm writing
a unit tests for the rather old proprietary OO application that has all
sort of code smells.
The code I want to test looks like this:
=cut=
use Corp::System; # exports runCommand()
use Logcheck;
sub new { ... }
sub createUser {
...
my $exitval = runCommand("useradd ...");
...
Logcheck::createLogDir();
...
}
=cut=
I want to override the runCommand method from Corp::System module. I've
found only one way to do it. In my unit test module I write:
=cut=
BEGIN {
use Corp::System;
package Corp::System;
sub runCommand { push @COMMANDS, shift; return 0; }
1;
}
=cut=
But this method is very boring, unelegant and error-prone, because sub
createUser, which I'm testing, uses subroutines from a lot of modules
and there're numerous dependencies between them.
Is there more elegant and flexible method for testing such classes
without refactoring them? In any case, tests should exist before
refactoring.
Eugene