Here's a common enough problem that I assume someone else has solved
it.

Tools in question:

  Test::Class
  Test::WWW::Mechanize

We want to test creating users (pseudo-code, but close to what we
actually use):

  sub add_user : Tests(5) {
      my $test = shift;
      my $mech = $test->mech;
      $mech->get_ok( $add_user_page, 'get the "Add User" page' );
      $mech->submit_form(
        form_name => 'add_user',
        fields    => { name => $some_name }
      );
      $mech->content_like( qr/User added/ );
      $mech->back;
      $mech->submit_form(
        form_name => 'add_user',
        fields    => { name => $some_other_name }
      );
      $mech->content_like( qr/User added/ );
      $mech->get_ok( $user_page, 'get the "List User" page' );
      $mech->content_like( qr/2 users found/ );
  }

So that's all well and good, but another class has another method where
we to add accounts, but we must have a customer backing each account
successfully added.  We'd rather not simply duplicate the above logic
and since the code's already written, I had the (quite possibly stupid)
idea of rewriting the above like this:

  sub add_user : Tests(5) {
      my $test = shift;
      my $mech = $test->mech;

      my $override = override_if_fixture(
          test => $test,
          mech => $mech,
          overrides => {
              get_ok       => sub {
                  my ( $mech, $page ) = @_;
                  $mech->get($page);
              },
              content_like => sub {},
          }
      );
      $mech->get_ok( $add_user_page, 'get the "Add User" page' );
      $mech->submit_form(
        form_name => 'add_user',
        fields    => { name => $some_name }
      );
      $mech->content_like( qr/User added/ );
      $mech->back;
      $mech->submit_form(
        form_name => 'add_user',
        fields    => { name => $some_other_name }
      );
      $mech->content_like( qr/User added/ );
      $mech->get_ok( $user_page, 'get the "List User" page' );
      $mech->content_like( qr/2 users found/ );
  }

The idea is, in this scope, we'd convert the 'get_ok' and
'content_like' methods to *not* be tests for the current scope.  This
allows us to use our tests as test fixtures rather than duplicate this
logic.

I think this is a horribly clumsy idea, but the above is a simplified
example and some test data could get hairy to setup and if we change
our Web pages, I'd hate to find all of the duplicated logic for setting
up this test data and testing that it actually works (note that we
could be testing a remote server and not always be in a position to
access the database directly).

Thoughts?

Cheers,
Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/

Reply via email to