Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

How to Test When User Sees Same Thing?

by Anonymous Monk
on Sep 09, 2020 at 12:08 UTC ( #11121508=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Please forgive the ambiguous title, but I'm not sure how to phrase this.

I have what must be a common scenario: I validate some input based on various factors, but if validation fails, the user sees the same error message. (That is, this is not a case where the user gets a message saying "Please fix field #3".) How do I write tests for each possibility, given that there's no difference in the program's function for any particular kind of failure?

if ( ! defined $input_object) { $self->redirect('error'); } elsif ($input_object->{'foo'} > 10) { $self->redirect('error'); } elsif ($input_object->{'bar'} !~ m/^\d{3}$/ ){ $self->redirect('error'); } $self->process($input_object);

It's important to test each of these cases (an undefined input object, a 'foo' input greater than 10, a 'bar' input that's not three digits) to make sure they fail, but I don't know how to make sure I'm testing each thing when the result of each one is the same.

Replies are listed 'Best First'.
Re: How to Test When User Sees Same Thing?
by hippo (Chancellor) on Sep 09, 2020 at 12:55 UTC

    Here are some things you might consider depending upon what it is you really want to achieve.

    1. Make the results different for the user (then the problem goes away). Why not tell the user that the reason for the error is that foo is more than 10 - isn't this useful information to them?
    2. If you want to continue to hide the specific error from the user for some reason then simply add in a differing response for your own use which the user won't see. This might be:
      1. Throw different exceptions
      2. Raise different warnings
      3. Return different values from the subroutine
    3. Use Devel::Cover to ensure that your test suite is exercising all the possibilities. You do have a test suite, don't you?


Re: How to Test When User Sees Same Thing?
by BillKSmith (Prior) on Sep 09, 2020 at 22:22 UTC
    You seem to want the same error processing if any of the three errors occur. Here is a slightly more compact way to write that:
    use strict; use warnings; my $input_object = { foo => 11, bar => '1234', }; my $self; if ( ( !defined $input_object ) or ( $input_object->{foo} > 10 ) or ( $input_object->{bar} !~ m/^\d{3}$/ ) ) { self->redirect('error'); }
Re: How to Test When User Sees Same Thing?
by GrandFather (Sage) on Sep 09, 2020 at 21:10 UTC

    Instead of testing the application as a whole (integration testing), consider writing tests for small parts of the application (unit testing). Both types of testing are important, but unit testing will find issues and help you solve them much faster than integration testing.

    Modules such as Test2::Suite make it fairly easy to write a suite of unit and integration tests.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11121508]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2020-10-24 09:46 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (242 votes). Check out past polls.