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

I'm doing some test-driven development using Test::More, and have a few initial single-step tests but any following tests are at least 2 stages:

So far, I've worked out the following code which fails with a syntax error.
use Test::More; ok( my $google = Google::Auth->login(email => 'test@gmail.com', password => 'pass', service => 'cp'); my $contactlist = Google::ContactList->fetchall($google); );

Whats the correct way to create this type of test?


just another cpan module author

Replies are listed 'Best First'.
Re: Writing multiple-stage tests
by Narveson (Chaplain) on May 07, 2010 at 03:47 UTC

    Write two tests.

    my $google = Google::Auth->login( email => 'test@gmail.com', password => 'pass', service => 'cp', ); isa_ok $google, 'Google::Auth'; my $contactlist = Google::ContactList->fetchall($google); isa_ok $contactlist, 'Google::ContactList';

    Here, I've tested whether these two method calls successfully constructed instances of their classes. I don't know whether these methods are really supposed to be constructors, but at least you can tell what is being tested. The isa_ok function is a handy abbreviation for

    ok( $google->isa('Google::Auth'), 'Constructed an instance of Google::Auth', );

    Then write a few more tests to show that these objects do what they're supposed to do.

Re: Writing multiple-stage tests
by ikegami (Patriarch) on May 07, 2010 at 05:26 UTC
    You can have as much prep code as you want between calls to ok (and the like). There's nothing special about ok. It's just a function that checks if the value you passed to it is true or not, and prints out something appropriate in response.
    my ($x, $y) = (1, 2); is($x, 2, 'x'); is($y, 3, 'y'); $x += 4; is($x, 6, '+=');

    (I used is instead of ok since there's rarely any reason to use ok.)