About Kent Cowgill
Articles filed under...
abs ab_ripper andylester arms back baggyshorts bestpractices biceps bike birthday blog bugs bus calculator cardio catalyst cgi chart chest chinups code cpan datamodel dbi doctor documentation exercise exhaustion fitness flattire flat_tire google gps heart_rate helmet history home houston html humor journal kate kenpo kenpo_x kettlebell knees lazy legs lisa lisanne maps math matthew michaelmckenna mom montreal motivation movie mysql oops orm P90X pain park patellar_tendonitis patrick pdf perl phb photos physical_therapy plyometrics poor_gait presentation procrastination progress pullups pushups pyramid rabbits racecondition rant refactor rest ribs ride route running shoulders situps slides sore spike sql statistics syntax test testing textile timex training triceps ups versioncontrol video vim vimrc walk warren work workouts yapc yapcna2007 yoga youtube

A R C H I V E S

(3)
(1)
(3)
(2)
(7)
(15)
(16)
(25)
(3)
(4)
(2)
(4)
(11)
(1)
(1)
(3)
(2)
(2)
(10)
(5)
(2)
(3)
(4)
(9)
(21)
(3)
(3)
(1)
(6)
(4)
(1)
(4)
(3)
(2)
(1)


    Is Kent Cowgill Online?
    View Kent Cowgill's profile on LinkedIn
    Add to Technorati Favorites

    Recent Entries...

    Re: Re: Yoga kicks my butt

    Chris @ 46: Tatyana @ 21 – at best its a stop gap measure...

    Re: Vibram FiveFingers FTW

    Hats off to whoever wrote this up and potesd it....

    Re: A little more detail on using a new model

    百度 [url=http://www.sina.com]sina[/url] ...

    Re: Catching up through week 7

    testing video ...

    Re: Porting a non-Moose object to Moose

    Wow, look what I found, greedy genius ...

    Re: Porting a non-Moose object to Moose

    Kevin, You're right, that does seem a little confusing. ...

    Re: Porting a non-Moose object to Moose

    Wait. I'm confused. Moose isn't the tool to reach for. So...

    Re: Porting a non-Moose object to Moose

    You should switch to MooseX::Types to declare your Typed and...

    Porting a non-Moose object to Moose

    I'm currently working with a lot of legacy code in an envi...

    Testing strategy for mocking code

    I keep finding myself using the following idiom for writing ...

    weblog | `web·lôg -läg |
    noun
    Another term for BLOG
    ORIGIN 1990s: from web in the sense [World Wide Web] and log in the sense [regular record of incidents.]
    blog | bläg |
    noun
    A web site on which an individual or group of users produces an ongoing narrative.
    ORIGIN a shortening of WEBLOG.

    Testing strategy for mocking code

    Kent Cowgill

    I keep finding myself using the following idiom for writing unit tests in perl for modules that have to interact with other modules.

    use strict;
    use warnings;

    use Test::More tests => 4;

    {
      no warnings qw/redefine once/;
      local *ModuleToMock::MethodToMock = sub {
        ok( 1, 'got a call to the method' );
        is( ref $_[1], 'HASH', 'got an hashref as an arg' );
        is( $_[1]->{param}, 'value', 'got a reasonable value as a param' );
        return 'sensible return value';
      };

      my $obj = ModuleThatsBeingTested->new();

      my $return = $obj->OtherMethodThatCallsMockedModule(
        param_passed_along => { param => 'value' }
      );

      like( $return, qr/sensible return/, 'got a reasonable return' );
    }


    Yes, I know about Test::MockObject but for some reason I never reach for it these days. For comparison purposes, using Test::MockObject in the above example might look like:

    use strict;
    use warnings;

    use Test::More tests => 4;

    use Test::MockObject;

    my $mock = Test::MockObject->new();

    $mock->fake_module(
      'ModuleToMock',
      MethodToMock => sub {
        ok( 1, 'got a call to the method' );
        is( ref $_[1], 'HASH', 'got an hashref as an arg' );
        is( $_[1]->{param}, 'value', 'got a reasonable value as a param' );
        return 'sensible return value';
      },
    );

    my $obj = ModuleThatsBeingTested->new();

    my $return = $obj->OtherMethodThatCallsMockedModule(
      param_passed_along => { param => 'value' }
    );

    like( $return, qr/sensible return/, 'got a reasonable return' );

    I'm not sure what caused me to stop using it, though. I've heard recently (in fact, as I was writing this post and pondering its use):

    NOOOOOOOO! it loads UNIVERSAL::isa and ::can. If I want to mock something I make a mock class.

    But I'm sure that never entered my thought process.

    Maybe I just thought it was too much setup, even though it's really only a couple of lines of code more.

    Maybe it's because I can never remember the exact syntax for setting something up with it, without consulting either previous test code I've written or the documentation. I had to check both before posting this.

    Related Photos: perl

    Main Page | Login

    Do you want to buy me ? Find more gift ideas at my wishlist