in reply to Testing with Test::Mock::HTTP::Tiny
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Testing with Test::Mock::Tiny::HTTP
by haukex (Archbishop) on Sep 27, 2023 at 09:26 UTC | |
by bliako (Abbot) on Sep 27, 2023 at 10:19 UTC | |
by Bod (Parson) on Sep 27, 2023 at 14:39 UTC | |
by bliako (Abbot) on Sep 28, 2023 at 09:22 UTC | |
by Bod (Parson) on Sep 28, 2023 at 11:12 UTC | |
by Bod (Parson) on Sep 27, 2023 at 20:33 UTC | |
by bliako (Abbot) on Sep 28, 2023 at 09:47 UTC | |
by Bod (Parson) on Sep 29, 2023 at 21:38 UTC | |
by Bod (Parson) on Sep 28, 2023 at 11:08 UTC | |
| |
|
Re^2: Testing with Test::Mock::Tiny::HTTP
by Bod (Parson) on Sep 27, 2023 at 14:26 UTC | |
|
Re^2: Testing with Test::Mock::Tiny::HTTP
by Bod (Parson) on Sep 27, 2023 at 14:31 UTC |