Skip to content

Instantly share code, notes, and snippets.

@kwakwaversal
Last active September 18, 2017 14:33
Show Gist options
  • Save kwakwaversal/f716daec8cfb9aabcd78 to your computer and use it in GitHub Desktop.
Save kwakwaversal/f716daec8cfb9aabcd78 to your computer and use it in GitHub Desktop.
How to stub Mojo::UserAgent the right way #mojo #perl
package main;
use Mojo::Base -strict;
use Test::More;
use Mojo::UserAgent;
use Mojo::Transaction;
# preparing... monkey patch is a good style
no warnings 'redefine';
local *Mojo::UserAgent::get = sub {
my $tx = Mojo::Transaction->new;
$tx->res->body('<html><body>Hello</body></html>');
return $tx;
};
# testing
my $dom = Mojo::UserAgent->new->get('http://foo.bar')->res->dom;
is($dom->at('body')->text, 'Hello');
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment