Test::Mojo::Role::TestDeep - Add Test::Deep methods to Test::Mojo::WithRoles
version 0.007
use Test::Mojo::WithRoles 'TestDeep'; use Test::Deep; # Get Test::Deep comparison functions my $t = Test::Mojo::WithRoles->new( 'MyApp' ); # Test JSON responses with Test::Deep $t->get_ok( '/data.json' ) ->json_deeply( superhashof( { foo => 'bar' } ), 'has at least a foo key with "bar" value', ); # Test HTML with Test::Deep $t->get_ok( '/index.html' ) ->text_deeply( 'nav a', [qw( Home Blog Projects About Contact )], 'nav link text matches site section titles', ) ->attr_deeply( 'nav a', href => [qw( / /blog /projects /about /contact )], 'nav link href matches site section URLs', );
This module adds some Test::Deep functionality to Test::Mojo. Test::Deep allows for extremely-customizable testing of data structures. This module adds some helper methods to Test::Mojo (using Test::Mojo::WithRoles) to test your web app's responses using Test::Deep.
Test::Deep
Test::Mojo
$t->json_deeply( $expect, $desc ) $t->json_deeply( $ptr, $expect, $desc )
Test that the current response (parsed as a JSON object) matches the given tests. $expect is a data structure containing Test::Deep comparisons to run. $desc is an optional description of the test.
$expect
$desc
If given, $ptr is a JSON pointer string to pick out a single part of the data structure. This is more convenient than using Test::Deep's comparison routines to do the same thing. See Mojo::JSON::Pointer.
$ptr
Corresponds to cmp_deeply in Test::Deep.
$t->text_deeply( $selector => $expect, $desc );
Test the text of the elements matched by the given $selector against the given test. $expect is a data structure containing Test::Deep comparisons to run. $desc is an optional description of the test.
$selector
The elements will always be an arrayref, even if only one element matches.
For example:
# test.html <nav> <ul> <li><a href="/">Home</a></li> <li><a href="/blog">Blog</a></li> <li><a href="/projects">Projects</a></li> </ul> </nav> # test.t $t->get_ok( 'test.html' ) ->text_deeply( 'nav a' => bag( qw( Home Blog Projects ) ), 'nav element text is correct', );
This is equivalent to:
$t->get_ok( 'test.html' ); my $dom = $t->tx->res->dom; cmp_deeply [ $dom->find( 'nav a' )->map( 'text' )->each ], bag( qw( Home Blog Projects ) ), 'nav element text is correct';
$t->all_text_deeply( $selector => $expect, $desc );
Test the complete text of the elements and all child elements matched by the given $selector against the given test. $expect is a data structure containing Test::Deep comparisons to run. $desc is an optional description of the test.
# test.html <nav> <ul> <li><a href="/"><em>Home</em></a></li> <li><a href="/blog">Blog</a></li> <li><a href="/projects"><strong>Projects</strong></a></li> </ul> </nav> # test.t $t->get_ok( 'test.html' ) ->all_text_deeply( 'nav a' => bag( qw( Home Blog Projects ) ), 'nav element text is correct', );
$t->get_ok( 'test.html' ); my $dom = $t->tx->res->dom; cmp_deeply [ $dom->find( 'nav a' )->map( 'all_text' )->each ], bag( qw( Home Blog Projects ) ), 'nav element text is correct';
$t->attr_deeply( $selector, $attr => $expect, ..., $desc );
Test the given attributes of the elements matched by the given selector against the given test. $expect is a data structure containing Test::Deep comparisons to run. $desc is an optional description of the test.
The element attributes will always be an arrayref, even if only one element matches.
# test.html <form action="/search" method="GET"> ... </form> # test.t $t->get_ok( 'test.html' ) ->attr_deeply( 'form', action => [qw( /search )], method => [re( qr( get )i )], 'form element is correct', );
$t->get_ok( 'test.html' ); my $dom = $t->tx->res->dom; cmp_deeply [ $dom->find( 'form' )->map( attr => 'action' )->each ], [ qw( /search ) ], 'form element action is correct', ; cmp_deeply [ $dom->find( 'form' )->map( attr => 'method' )->each ], [ re( qr( get )i ) ], 'form element method is correct', ;
Doug Bell <preaction@cpan.org>
Doug Bell <madcityzen@gmail.com>
This software is copyright (c) 2015 by Doug Bell.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Test::Mojo::Role::TestDeep, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Mojo::Role::TestDeep
CPAN shell
perl -MCPAN -e shell install Test::Mojo::Role::TestDeep
For more information on module installation, please visit the detailed CPAN module installation guide.