Giter VIP home page Giter VIP logo

test-mojo-role-testdeep's Introduction

NAME

Test::Mojo::Role::TestDeep - Add Test::Deep methods to Test::Mojo::WithRoles

VERSION

version 0.007

STATUS

Coverage Status

SYNOPSIS

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',
  );

DESCRIPTION

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.

METHODS

json_deeply

$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.

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.

Corresponds to cmp_deeply in Test::Deep.

text_deeply

$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.

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';

all_text_deeply

$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.

The elements will always be an arrayref, even if only one element matches.

For example:

# 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',
  );

This is equivalent to:

$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';

attr_deeply

$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.

For example:

# 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',
  );

This is equivalent to:

$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',
    ;

SEE ALSO

AUTHOR

Doug Bell [email protected]

CONTRIBUTOR

Doug Bell [email protected]

COPYRIGHT AND LICENSE

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.

test-mojo-role-testdeep's People

Contributors

preaction avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.