Skip to content

Commit

Permalink
Basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
symkat committed Nov 26, 2021
1 parent 1a89f12 commit 7e7eb9e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Web/lib/Test/Mojo/BlogDB.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
package Test::Mojo::BlogDB;
# This package subclasses Test::Mojo and sets up some
# functionality.
#
# _stash_
# The $t object will now have a stash method that returns
# the stash.
#
# _code_block_
# The $t object will now have a code_block method that
# accepts a code block, runs it, and returns $t.
#
# This combination of stash and code_block enable a
# pattern like the following:
#
# $t->post_ok( '/login', { user => $user, pass => $pass})
# ->code_block( sub {
# my $t = shift;
# is($t->stash->{person}->user, $user, "User saved in stash.");
# })->status_is( 200 );
#
# _dump_stash_
# The $t object now has a dump_stash method that prints the
# stash to STDERR. By default this will supress mojo-specific
# stash elements, pass a true value to dump the full stash.
#
# $t->get_ok('/')
# ->dump_stash(1)
# ->status_is(200);
#
use parent 'Test::Mojo';
use Data::Dumper;
use Test::Deep;

sub new {
my $class = shift;
Expand Down Expand Up @@ -28,4 +58,33 @@ sub code_block {
return $t;
}

sub dump_stash {
my ( $t, $show_all ) = @_;

if ( $show_all ) {
warn Dumper $t->stash;
return $t;
}

foreach my $key ( keys %{$t->stash}) {
next if $key eq 'controller';
next if $key eq 'action';
next if $key eq 'cb';
next if $key eq 'template';
next if $key =~ m|^mojo\.|;

$ds->{$key} = $t->stash->{$key};
}

warn Dumper $ds;

return $t;
}

sub stash_has {
my ( $t, $expect, $desc ) = @_;

cmp_deeply( $t->stash, superhashof($expect), $desc);
}

1;
36 changes: 36 additions & 0 deletions Web/t/01_endpoints/01_root/02_register/01_basic.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env perl
use Mojo::Base '-signatures';
use BlogDB::Web::Test;

my $t = Test::Mojo::BlogDB->new('BlogDB::Web');

# Try creating an account with an error, ensure we get the error.
$t->post_ok( '/register', form => {
username => 'fred',
email => 'fred@blog.com',
password => 'SuperSecure',
confirm => 'SuperFail',
})->status_is( 200
)->code_block( sub {
is( shift->stash->{errors}->[0], 'Password & Confirmation must match.', 'Expected error thrown' );
})->code_block( sub {
is( shift->app->db->resultset('Person')->search( { username => 'fred'})->count, 0, 'No user created.');
});

# Try creating a valid account, ensure it exists in the DB.
$t->post_ok( '/register', form => {
username => 'fred',
email => 'fred@blog.com',
password => 'SuperSecure',
confirm => 'SuperSecure',
})->status_is( 302
)->code_block( sub {
is( scalar(@{shift->stash->{errors}}), 0, 'No errors' );
})->code_block( sub {
is( shift->app->db->resultset('Person')->search( { username => 'fred'})->count, 1, 'User created.');
})->get_ok( '/'
)->code_block( sub {
is(shift->stash->{person}->username, 'fred', 'Got the fred after login...');
})->stash_has( { blogs => [ ]}, "Blog array ref exists.");

done_testing();

0 comments on commit 7e7eb9e

Please sign in to comment.