# 名称
# 概述
使用Mojo::Base-strict; 使用测试::Mojo; 使用测试::更多; #启动一个名为“Celestial”的Mojolicious应用程序 my$t=测试::Mojo->new(“天国”); #发布JSON文档 $t->post_ok('/notifications'=>json=>{event=>'full-moon'}) ->状态_is(201) ->json_is('/message'=>'通知已创建'); #执行GET请求并查看响应 $t->get_ok(“/日出”) ->状态_ is(200) ->内容类(qr/am$/); $t->get_ok(“/日落”) ->状态_ is(200) ->内容类(qr/pm$/); #发布URL编码的表单 $t->post_ok('/insurance'=>form=>{name=>'Jimmy',金额=>'3.000.000'}) ->状态_is(200); #使用Test::More’s like()检查响应 比如$t->tx->res->dom->at('div#thanks')->text,qr/thankyou/,'thanks''; done_testing();
# 概念
# 测试::Mojo概览
$t->get_ok('/framgs'); $t->status_is(200); $t->content_like(qr/牛蛙/); $t->content_like(qr/amlytoad/);
$t->get_ok('/framgs') ->状态_ is(200) ->内容类(qr/牛蛙/) ->类内容(qr/催眠蟾蜍/);
$t->put_ok('/bees'=>json=>{type=>'worker',name=>'Karl'}) ->状态_ is(202) ->json_has('/id'); #从响应中提取id my$newbee=$t->tx->res->json('/id'); #使用上一响应中的数据发出新请求 $t->get_ok(“/bees/$newbee”) ->状态_ is(200) ->json_is('/name'=>'Karl');
#第一笔交易 $t->get_ok('/frogs?q=bullfrog'=>{内容类型'=>'application/json'}) ->状态_ is(200) ->类json('/0/species'=>qr/catebeianus/i); #仍是第一笔交易 $t->content_type_is(“应用程序/json”); #第二笔交易 $t->get_ok('/framgs?q=banjo'=>{内容类型'=>'text/html'}) ->状态_ is(200) ->类内容(qr/interioris/i); #还是第二笔交易 $t->content_type_is('text/html');
# Test::Mojo对象
my$t=测试::Mojo->new;
$t->get_ok(' https://www.google.com/ ') ->状态_ is(200) ->类内容(qr/search/i);
我的$tx=$t->ua->帖子(' https://duckduckgo.com/html '=>形式=>{q=>“催眠蟾蜍”}); $tx->result->dom->find('a.result__a')->each(sub{say$_->text});
# 测试Mojolicios应用程序
#侦听本地主机:32114(一些未使用的TCP端口) my$t=测试::Mojo->new(“青蛙”);
#加载相对于“t”目录的应用程序脚本 使用Mojo::File qw(curfile); my$t=Test::Mojo->new(curfile->dirname->sibling('myapp.pl'));
#改写为“ http://localhost:32114/frogs(本地主机:32114/青蛙) " $t->get_ok('/framgs');
# 一个例子
. The
$mojo生成应用程序MyApp [mkdir]/my_app/script [写入]/my_app/script/my_aapp [chmod]/my_app/script/my_ap 744 ... [mkdir]/我的应用/t [写入]/my_app/t/basic.t ...
$cd我的应用程序 $mkdir日志 $证明-lv t t/basic.t。。 确定1-获取/ ok 2-200 ok ok 3-内容类似 1..3 好 啊 所有测试均成功。 文件=1,测试=3,0壁钟秒(0.03 usr 0.01 sys+0.33 cusr 0.07 csys=0.44 CPU) 结果:通过
使用Mojo::Base-strict; 使用测试::更多; 使用测试::Mojo; my$t=测试::Mojo->new(“MyApp”); $t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i); done_testing();
# 使用配置数据进行测试
#从“my_app.conf”返回的散列加载配置 my$config=$self->插件('config'); #控制器的正常路径 $r->get('/')->to('example#welcome'); #新增:只有在配置中设置了“enable_weather”时,该路线才存在 if($config->{enableweather}){ $r->get(“/天气”=>sub($c){ $c->render(text=>“很热! 🔥 "); }); }
my$t=Test::Mojo->new(MyApp=>{enable_weather=>1}); $t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i); $t->get_ok('/weather')->status_is(200)->content_like(qr/ 🔥/);
# 测试应用程序助手
使用Mojo::Util qw(b64_encode); app->helper(basic_auth=>sub($c,@values){ return{Authorization=>“Basic”.b64_encode联接(“:”=>@values),“”}; });
my$t=测试::Mojo->new(“MyApp”); is_deeply$t->app->basic_auth(bif=>“bif's Passwerdd”),{Authorization=>'基本YmlmOkJpZidzIFBhc3N3ZXJkZA=='}, '正确的标题值';
# 资产
HTTP请求 HTTP响应状态 HTTP响应标头 HTTP响应内容/正文
# HTTP请求断言
# 使用HTTP请求断言
$t->post_ok('/calls'=>form=>{to=>'+43.55.5555555'});
POST/调用HTTP/1.1 内容物长度:20 内容类型:application/x-www-form-urlencoded 到=%2B43.55.555.555
$t->get_ok('/internal/personmental'=>{Authorization=>'Token secret-password'}=>form=>{q=>'Professor Plum'});
GET/内部/人员? q=教授+梅花HTTP/1.1 内容长度:0 授权:Token secret-password
# HTTP响应状态代码
$t->post_ok('/doorbell'=>form=>{action=>'ring-once'}) ->状态_is(200);
$t->post_ok('/doorbell'=>form=>{action=>'ring-once'}); 是$t->tx->res->message,‘Moved Permanently’,‘try next door’;
# HTTP响应标头
$t->get_ok('/map-of-the-world.pdf') ->content_type_is('应用程序/pdf');
$t->get_ok('/map-of-the-world.pdf') ->header_is(“内容类型”=>“应用程序/pdf”);
$t->get_ok('/map-of-the-world.pdf') ->content_type_is('应用程序/pdf') ->header_isnt(“压缩”=>“gzip”) ->header_unliverse(“服务器”=>qr/IIS/i);
# HTTP响应内容断言
$t->get_ok('/scary-things/spiders.json') ->content_is('{“蜘蛛”:“棕色隐士”}');
$t->get_ok('/scary-things/spiders.html') ->content_like(qr{<title>所有蜘蛛</title>});
# JSON响应断言
$t->get_ok('/alives/friendy.json') ->json_has('/beens/jeremiah/age');
$t->get_ok('/alives/friendy.json') ->json哈斯('/beens/jeremiah/age') ->json_is('/beens/jeremiah/age'=>42) ->类json('/beens/jeremiah/species'=>qr/牛蛙/i);
# DOM响应断言
$t->text_is('div.foo[x=y]'=>“你好!”); $t->text_is('html head title'=>'Hello!','right title');
# 高级主题
# 重定向
获取/1
找到302个 位置:/2
获得/2
找到302个 位置:/3
获得/5
200正常 {“消息”:“这是五”}
my$t=测试::Mojo->new; $t->get_ok('/1') ->header_is(位置=>'/2'); $t->ua->max_redirects(1); $t->get_ok('/1') ->header_is(位置=>“/3”); $t->ua->max_redirects(2); $t->get_ok('/1') ->header_is(位置=>“/4”); #查看上一跳 是$t->tx->previous->res->headers->location,“/3”,“previous redirect”; $t->ua->max_redirects(3); $t->get_ok('/1') ->header_is(位置=>'/5'); $t->ua->max_redirects(4); $t->get_ok('/1') ->json_is('/message'=>'这是五个');
# Cookie和会话管理
使用Mojo::Base-strict; 使用测试::更多; 使用测试::Mojo; my$t=测试::Mojo->new(“MyApp”); #无授权cookie $t->get_ok('/') ->状态(401) ->content_is('请登录'); #应用程序设置授权cookie $t->post_ok('/login'=>form=>{password=>'let me in'}) ->状态_ is(200) ->content_is('您已登录'); #从上一个事务发送cookie $t->get_ok('/') ->状态_ is(200) ->content_like(qr/您在\d+/登录); #清除Cookie $t->reset_session; #再次没有授权cookie $t->get_ok('/') ->状态(401) ->content_is('请登录');
$t->get_ok('/'); 比如$t->tx->res->cookie('smarty'),qr/smarty=pants/,'cookie found';
# 自定义交易记录
#使用自定义“RING”动词 my$tx=$t->ua->build_tx(环=>“/门铃”); #设置一个特殊的cookie $tx->req->cookie({name=>'Secret',value=>“不要告诉任何人”}); #提出请求 $t->request_ok($tx) ->状态_ is(200) ->json_is('/status'=>'ding-dong');
# 测试WebSocket web服务
使用Mojo::Base-strict; 使用测试::更多; 使用测试::Mojo; #测试echo web服务 my$t=测试::Mojo->new(“EchoService”); $t->websocket_ok('/echo') ->send_ok(“你好,Mojo!”) ->消息_ok ->message_is('echo:你好,Mojo!') ->finish_ok; #测试JSON web服务 $t->websocket_ok('/echo.json') ->send_k({json=>{test=>[1,2,3]}}) ->消息_ok ->json消息('/test'=>[1,2,3]) ->finish_ok; done_testing();
# 扩展测试::Mojo
包测试::Mojo::Role::Location; 使用Mojo::Base-role,-signatures; sub-location_is($self,$value,$desc=“位置:$value”){ 返回$self->test('is',$self->tx->res->headers->location,$value,$desc); } 1;
my$t=测试::Mojo->with_roles('+Location')->new('MyApp'); $t->post_ok('/redirect/mojo'=>json=>{message=>'mojo,我来了!'}) ->状态(302) ->location_is(' http://mojolicious.org ') ->或(子{diag“我想念tempire”});