Test::Mock::HTTP::Tiny's set_mocked_data() expects an arrayref or a hashref. You are providing it with a scalar (string).

The captured data is indeed an arrayref as Test::Mock::HTTP::Tiny->captured_data_dump shows. But what you save to a file is a text representation of a Perl data structure. What you get when you read that data back from file is a scalar string. And that's what you pass to Test::Mock::HTTP::Tiny's set_mocked_data() which silently ignores you!

You need to "undump" the file contents somehow and resurrect the Perl data structure you started with. The simplest way to achieve this is by eval()'ing the text representation minus the $VAR1 = at the beginning of the string (placed there by Data::Dumper) (see the warnings at the end of this post):

$replay =~ s/^\$VAR1\s*=\s*//; $replay = eval $replay; print "ref: ".ref($replay)."\n"; Test::Mock::HTTP::Tiny->set_mocked_data($replay);

You were unlucky in debugging -- in the source code of Test::Mock::HTTP::Tiny I see this:

if (ref($new_mocked_data) eq 'ARRAY') { ... elsif (ref($new_mocked_data) eq 'HASH') { ... else { # TODO: error }

Apropos HTTP mocking, https://blogs.perl.org/users/e_choroba/2016/01/post.html by choroba presents an alternative.

WARNING: Be warned that eval()'ing things from files is a huge security hole. An alternative would be to use Storable to dump/freeze and resurrect/thaw a Perl data structure. But it too has boldface security warnings:

my $mocked_data = Test::Mock::HTTP::Tiny->mocked_data(); store $mocked_data, 'mock_html.dat'; ... my $replay = retrieve 'mock_html.dat'; print "ref: ".ref($replay)."\n"; Test::Mock::HTTP::Tiny->set_mocked_data($replay);

bw, bliako


In reply to Re: Testing with Test::Mock::HTTP::Tiny by bliako
in thread Testing with Test::Mock::HTTP::Tiny by Bod

Title:
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, details, 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, summary, 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.