Test::Mock::LWP ã試ã
ä¹ ãã¶ãã®ã¨ã³ããªã§ããä¸å¿å æ°ã«ãã£ã¦ã¾ããã£ã¨ã
ããã¾ã§ã®ãããã
ååã® id:typomaster ãããããTest::Mock::LWP ã使ã£ã¦ãã¹ãæ¸ããããã ãã©ãã¨è¨ãããã®ã§æ©é軽ã試ãã¦ã¿ã¾ãããã
Test::Mock::LWP ã¨ã¯
å ·ä½çã«ã¯ã
- LWP::UserAgent
- HTTP::Request
- HTTP::Response
ã¢ã¸ã¥ã¼ã«ã«å¯¾ã㦠Test::MockObject åããç©ã ã¨èãã¦è¯ããããããã
- Test::Mock::LWP::UserAgent
- Test::Mock::HTTP::Request
- Test::Mock::HTTP::Response
ã¢ã¸ã¥ã¼ã«ã対å¿ãã¦ãã¾ããããããã½ã¼ã¹ã¯çãã®ã§å®éã«è¦ã¦ã¿ã¾ãããã
Test::Mock::HTTP::Request
package Test::Mock::HTTP::Request; use strict; use warnings; use Test::MockObject; use base 'Exporter'; our @EXPORT = qw($Mock_req $Mock_request); our $Mock_req; our $Mock_request; our $VERSION = '0.01'; BEGIN { $Mock_request = $Mock_req = Test::MockObject->new; $Mock_req->fake_module('HTTP::Request', new => sub { $Mock_req->{new_args} = [@_]; $Mock_req }); } $Mock_req->set_always('authorization_basic', ''); $Mock_req->set_always('header', ''); $Mock_req->set_always('content', ''); sub new { $Mock_req }; $Mock_req->mock('-new_args', sub { delete $Mock_req->{new_args} }); package HTTP::Request; our $VERSION = 'Mocked';
ã¾ãããåãããã©ã$Mock_req, $Mock_request ã£ã¦ã®ã¯åãå¤ã¨ã㦠Test::MockObject ã®ã¤ã³ã¹ã¿ã³ã¹ãä»£å ¥ããã¦ãã¦ãHTTP::Request ã fake ãã¾ããã¨ãnew ããæ㯠new_args ãã£ã¼ã«ãã«é åãªãã¡ã¬ã³ã¹ã¨ãã¦å¼æ°ãæ ¼ç´ãã¾ãã
set_always ã£ã¦ã®ã¯æå®ããã¡ã½ããã®æ»ãå¤ã®ããã©ãã©ããããã£ã¦è©±ã
Test::Mock::HTTP::Response
ããã¤ã Request ã¨å¤§ä½ä¼¼ããããªæã
package Test::Mock::HTTP::Response; use strict; use warnings; use Test::MockObject; use base 'Exporter'; our @EXPORT = qw($Mock_resp $Mock_response); our $Mock_resp; our $Mock_response; our $VERSION = '0.01'; BEGIN { $Mock_response = $Mock_resp = Test::MockObject->new; $Mock_resp->fake_module('HTTP::Response'); $Mock_resp->fake_new('HTTP::Response'); } our %Headers; $Mock_resp->mock('header', sub { return $Headers{$_[1]} }); $Mock_resp->set_always('code', 200); $Mock_resp->set_always('content', ''); $Mock_resp->set_always('is_success', 1); package HTTP::Response; our $VERSION = 'Mocked';
ãã£ãã¨å¤§ä½åãã ã£ããã ãã©ãä»å㯠new ãã¦ãå¼æ°ãæ ¼ç´ãããã¯ããªããour %Headers ãä¸æã使ãã°ã¬ã¹ãã³ã¹ãããã¨ãã®ã¢ã¯ã»ãµãç°¡åã«å®ç¾©ã§ãã¦ãå¾ã¯åæå¤ã¯ 200 OK ãªãã§ãããã§ãis_success 㯠1 ãè¿ãã¨ã
Test::Mock::LWP::UserAgent
package Test::Mock::LWP::UserAgent; use strict; use warnings; use Test::MockObject; use Test::Mock::HTTP::Response; use Test::Mock::HTTP::Request; use base 'Exporter'; our @EXPORT = qw($Mock_ua); our $Mock_ua; our $VERSION = '0.01'; BEGIN { $Mock_ua = Test::MockObject->new; $Mock_ua->fake_module('LWP::UserAgent'); $Mock_ua->fake_new('LWP::UserAgent'); } $Mock_ua->set_always('simple_request', HTTP::Response->new); $Mock_ua->set_always('request', HTTP::Response->new); package LWP::UserAgent; use strict; use warnings; our $VERSION = 'Mocked';
è¦ãéããrequest, simple_request ããå®ç¾©ãã¦ãªãã®ã§ãä»ã®ã¡ã½ããç使ãããå ´åã¯èªåã§ä½ã¨ããã¾ãã
Sample
ãªãããããã¡ãªå¦çããã¹ãã«ãã¦ã¿ããã
use Test::More; use Test::Mock::LWP; use Test::Mock::HTTP::Response; use LWP::UserAgent; use HTTP::Request; use HTTP::Response; plan tests => 6; $Mock_request->set_isa('HTTP::Request'); $Mock_response->set_isa('HTTP::Response'); $Mock_ua->set_isa('LWP::UserAgent'); $Mock_response->mock( content => sub { return << 'CONTENT'; He should write blog entry as soon as posibble. CONTENT } ); %Test::Mock::HTTP::Response::Headers = ( 'Content-Type' => 'text/plain', ); my $req = HTTP::Request->new(GET => 'http://example.com/'); isa_ok($req, 'HTTP::Request'); my $ua = LWP::UserAgent->new; isa_ok($ua, 'LWP::UserAgent'); my $res = $ua->request($req); isa_ok($res, 'HTTP::Response'); ok($res->is_success, 'is request() success'); is($res->code, 200, 'status code'); is($res->header('Content-Type'), 'text/plain', 'match Content-Type header'); note($res->content);
試ãã«å®è¡ãã¦ã¿ãã¨ã
$ prove -vc --timer typomaster.t [00:48:32] typomaster.t .. 1..5 ok 1 - The object isa HTTP::Request ok 2 - The object isa LWP::UserAgent ok 3 - The object isa HTTP::Response ok 4 - is request() success? ok 5 - status code # He should write blog entry as soon as posibble. ok 228 ms [00:48:33] All tests successful. Files=1, Tests=5, 1 wallclock secs ( 0.05 usr 0.09 sys + 0.17 cusr 0.06 csys = 0.37 CPU) Result: PASS
ã¿ããã«ãªãã
ã¡ãªã¿ã« LWP 使ã£ã¦ãæ¢åã®ã³ã¼ãã«ã Test::Mock::LWP ã使ã£ããã¹ããçµã¿è¾¼ãäºãåºæ¥ã¾ããã¾ãæåãè²ã åç¾ããä¸ã§ Test::MockObject ã®ä½¿ãæ¹ã¯ä½ã¨ãªãç¥ã£ã¦ãæ¹ãè¯ãã¨æãã¾ãã
ã¨ããããæ軽ã«ä½¿ãããã¨ã
ã¾ã¨ã
- çµç·¯ã¯ã¨ããããå©ãã¦ãç®æã ãåããã°ããã®ã§åºæ¬çã«ãæ軽
- ä½ãè£ãè¿ãã°å¤å°è¤éãªäºããã£ã¦ãéã«ã¯ãããä»ã«ãªããã
- Test::HTTP::Server::Simple ã¨ãã¨ä½¿ãåããã¨ãããã
ã¨ãããããããã°ã¡ããã¨æ¸ãã¦ãã ãã > id:typomaster