Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Great tutorial, and brilliant work! I completed your tutorial and read the rest of the documentation before I went beyond your celebrational espresso. The following is a condensed and simplified example of how I put your module to use in combination with chromatic's excellent Test::MockObject to test one of my own modules. Note that the enclosed snippet is untested, as i stripped away quite a bit of detail that I thought complicated the example.

In the web app I work on at $firm, we channel all database access through a single component. The module I want to test queries the database and ranks entities according to certain criteria, using a simple formula. What I want to test here is the method that does the ranking.

#! /usr/bin/perl use strict; use Test::LectroTest; use Test::MockObject; use List::Util qw( reduce ); use Score; BEGIN { # Faking the database component Test::MockObject->fake_module( 'My::Database' ); } # Making a mockobject of the database my $mock = Test::MockObject->new(); $mock->fake_new( 'My::Database' ); my $scorer = Score->new(); # The Score module expects the database results as hashes. # This closure generator allows me to mock the database # access method. sub hash_sequence { my @sequence = @_; my $index = 0; return sub { return %{ $sequence[ $index++ ] }; } } Property { ##[ input <- List( Int( range=>[50,100], sized=>1), length=>5 ) ]## # Calculate some 'constants' my $questions = reduce { $a + $b } @{ $input }; # Sum the result set my @data; for( my $i = 0; $i < 5; $i++ ) { # Feed the mocker some mock push( @data, { 1 => $input->[ $i ] } ); } # Setup mockobject $mock->mock( 'getHash', &hash_sequence( @data ) ); $scorer->setDatabase( $mock ); # Calculate expected result my $raw = ( ( $input->[ 2 ] + ( 2 * $input->[ 3 ] ) - $input->[ 0 ] ) + $questions ) / ( 3 * $questions ); my %result = $scorer->score(); # The method to test $raw == $result{ 1 }; }, name => "score holds.";

I should perhaps be a bit sceptical that this test passes. I haven't twiddled and played much with the generators yet, so raising my confidence in the module demands some more playing around with numbers.

I may of course have abused both Test::LectroTest and Test::MockObject. Abusing Test::LectroTest in that I actually calculate the expected score in the Property itself. This is of course error-prone, in I might introduce errors in the 'verification' code. When it comes to Test::MockObject, I must admit admit that I lack the experience to see the flaws in my logic. I'm sure some wiser monk will find something, though :)

When it comes to answering your question about oversights in the tutorial, then I agree with stvn that you should give the generators more focus. Especially some help on how to limit ranges, and combining generators into complex structures. I found the answers I needed in the the Test::LectroTest::Generator manual, though, so it worked great as a quick start.

I wondered where the name came from too, but after I found:

The result is LectroTest, a horribly named, automatic, specification-based testing tool for Perl.

on your homepage, I thought it better not to ask ;)

All in all, I believe Test::LectroTest will become a valuable tool at work. Thank you very much :)

pernod
--
Mischief. Mayhem. Soap.


In reply to Test::LectroTest and Test::MockObject. by pernod
in thread RFC: Tutorial on Testing by tmoertel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-18 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found