Last active
September 18, 2017 14:33
-
-
Save kwakwaversal/f716daec8cfb9aabcd78 to your computer and use it in GitHub Desktop.
How to stub Mojo::UserAgent the right way #mojo #perl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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