Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
At work, we use a rather old Perl on the servers (5.10.1 on RedHat 6, but still better than 5.8.3 at my $job-1), with similarly dated distributions. We plan to upgrade, so I tried running some of the tests locally on my laptop with Perl 5.18.2 (openSUSE Leap 42.1).

Hash randomisation

Most failures were caused by the new hash randomisation (see Hash order randomization is coming, are you ready?). As we use Test::Spec, most failures can be solved easily by turning an array reference into a bag:

# Old: cmp_deeply($obj->method, [ $result1, $result2 ]); # New: cmp_deeply($obj->method, bag($result1, $result2));

Mocking objects under Moose

We use Moose in most of the code, which makes mocking a bit harder because of type constraints. Imagine you have the following code you need to test:

use warnings; use strict; { package Person; use Moose; use namespace::autoclean; has name => ( is => 'rw', isa => 'Str', required => 1, ); has id => ( is => 'ro', isa => 'Str', required => 1, ); __PACKAGE__->meta->make_immutable; } { package Position; use Moose; use namespace::autoclean; has person => ( is => 'rw', isa => 'Person', ); has title => ( is => 'ro', isa => 'Str', ); __PACKAGE__->meta->make_immutable; }

When testing the Position, we don't care about the details of the Person. We only want to stub an object with the needed methods implemented, which can even be none:

use Test::Spec; describe 'position' => sub { it 'instantiates' => sub { my $person = stub(); my $position = 'Position'->new(person => $person); isa_ok($position, 'Position'); cmp_deeply([ $position->person ], bag($person)); }; };

(The last line doesn't make much sense in this context, but imagine more complex objects. We need to use the bag function somewhere to show the problem.)

This would work under plain OO, but it doesn't for us; Moose complains:

Attribute (person) does not pass the type constraint because: Validati +on failed for 'Person' with value Test::Spec::Mocks::MockObject={ }

The common trick to solve this, appearing all over the code base, has been to stub the isa method of the object to always return 1. Moose's got happy when checking the object type, and no one else has cared:

my $person = stub( isa => 1 );

But alas, this trick doesn't work in the newer Test::Deep. Here's the failure message:

Found a special comparison in $data You can only use specials in the expects structure at /home/choroba/pe +rl5/lib/perl5/Test/ line 346.

Pretty informative, don't you think? It took me several hours to find the exact reason; the indicated line was changed in May 2011 in the following way:

# Old: if (! $Expects and ref($d1) and UNIVERSAL::isa($d1, "Test::Deep::Cmp") +) # New: if (! $Expects and Scalar::Util::blessed($d1) and $d1->isa("Test::Deep +::Cmp"))

So, returning 1 from the isa method now makes Test::Deep believe the stubbed object is its special construct that shouldn't appear on the left hand side of the comparison.

To verify that's actually the problem, I tried to modify the isa in a more sophisticated way:

my $person = stub( isa => sub { $_[1] !~ /Test/ } );

and yes, it started to work again. Moose asks for Person , so isa returns 1, the testing framework asks for Test::Deep::Cmp and therefore gets 0.

Final solution

But it's an ugly hack. Some other modules might get confused by such a mocking, as they might check for other classes not containing Test , or, worse, we also have several classes whose namespace contains Test somewhere. So, I created a helper function

sub mock_isa { my ($class) = @_; isa => sub { $_[1] eq $class } }

which can be used as

my $person = stub(mock_isa('Person'));

Not as short as the original trick, but still easier than reimplementing the whole inheritance logic. What tricks do you use?

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

In reply to Mocking isa under Test::Deep by choroba

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (6)
    As of 2020-01-24 07:30 GMT
    Find Nodes?
      Voting Booth?