Ovid wrote:
> 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).

Ick.

I see where you're coming from but this idea sounds too much like...

sub foo {
    my(%args) = @_;

    if( $args{flag} ) {
        ...
    }
    else {
        ...
    }
}

That is, you have one routine doing two different things depending on if a flag 
is passed in.  Meaning you really have two routines.  Your routine is doing too 
much work and its name is wrong.  Its not "add_user" but 
"add_two_users_and_list_them".  The fact that its a test doesn't change this.  
Break it down into smaller more flexible pieces.

  sub add_user : Tests(2) {
      my $test = shift;
      my $user_name = 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 => $user_name }
      );
      $mech->content_like( qr/User added/ );
      $mech->back;
  }

  sub check_num_users: Test(2) {
      my $test = shift;
      my $num_users = shift;

      $mech->get_ok( $user_page, 'get the "List User" page' );
      $mech->content_like( qr/$num_users users found/ );
      $mech->back;
  }

  $test->add_user("Joe Foo");
  $test->add_user("Jim Bar");
  $test->check_num_users(2);

(You might be even better served by checking that those two specific users 
actually exist rather than just checking the number of users.  If you do that 
you can tack that test onto the end of add_user().)

Now you have two handy testing utility routines where before you had one 
single-purpose routine.  Use as desired.

  sub add_account {
      my $test = shift;
      my $account_num = shift;

      $test->add_user("Account Test User");

      ...add the account...
  }

Finally, why turn off the tests?  Why not just let them run?  What's the harm?  
Worried about messing up the count?  Use no_plan, its preferable to special 
hackery.

Reply via email to