Bod has asked for the wisdom of the Perl Monks concerning the following question:
I am writing some tests of a module that fetches a webpage using HTTP::Tiny->get
To test it, I am trying to use Test::Mock::HTTP::Tiny but I've never tried to use a Test::Mock module before. It's been on my radar since kcott mentioned their existence many moons ago. Now I have the need for one...but the documentation is lacking (to put it mildly!)
First I've run this code to get the mock data:
Then I've renamed all the references to the domain www.way-finder.uk (a real domain) to www.testing.crawl (a mock domain). I've done this because I don't want the tests going out to a live site as it will change over time invalidating the tests.use strict; use warnings; use HTTP::Tiny; use Test::Mock::HTTP::Tiny; my $http = HTTP::Tiny->new; my $resp = $http->get('http://www.way-finder.uk/'); open my $fh, '>', 'mock_html.dat'; print $fh Test::Mock::HTTP::Tiny->captured_data_dump; close $fh;
My test file looks like this:
The method $crawl->crawl uses HTTP::Tiny->get to get the web address.use strict; use warnings; use Test::More; use Test::Mock::HTTP::Tiny; use WWW::Crawl; plan tests => 1; $/ = undef; open my $fh, '<', 't/mock_html.dat' or die "Can't open datafile"; my $replay = <$fh>; close $fh; die "Nothing to replay" unless $replay; Test::Mock::HTTP::Tiny->set_mocked_data($replay); my $crawl = WWW::Crawl->new( 'timestamp' => 'a', ); my @links = $crawl->crawl('https://www.testing.crawl', \&link); cmp_ok ( scalar @links, '==', 8, 'Correct link count'); sub link { diag ($_[0]); }
My expectation was that Test::Mock::HTTP::Tiny would replay the website to HTTP::Tiny but instead it gives the HTTP error 599 - Internal Exception
Is this the right way to use Test::Mock objects or am I completely off track here?
EDIT:
Corrected typo in title
ANOTHER EDIT:
Corrected module in title!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Testing with Test::Mock::HTTP::Tiny
by bliako (Abbot) on Sep 27, 2023 at 08:06 UTC | |
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 27, 2023 at 20:33 UTC | |
by bliako (Abbot) on Sep 28, 2023 at 09:47 UTC | |
| |
by Bod (Parson) on Sep 27, 2023 at 14:26 UTC | |
by Bod (Parson) on Sep 27, 2023 at 14:31 UTC | |
|
Re: Testing with Test::Mock::Tiny::HTTP
by kcott (Archbishop) on Sep 27, 2023 at 11:18 UTC | |
by Bod (Parson) on Sep 27, 2023 at 18:17 UTC |