Skip to content

Commit

Permalink
added success attribute to Test::Mojo
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 3, 2014
1 parent c56a12a commit 2782efc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

4.66 2014-01-03
- Added success attribute to Test::Mojo.

4.65 2014-01-02
- Deprecated use of hash references for optgroup generation with
Expand Down
16 changes: 11 additions & 5 deletions lib/Test/Mojo.pm
Expand Up @@ -16,7 +16,7 @@ use Mojo::UserAgent;
use Mojo::Util qw(decode encode);
use Test::More ();

has [qw(message tx)];
has [qw(message success tx)];
has ua => sub { Mojo::UserAgent->new->ioloop(Mojo::IOLoop->singleton) };

# Silent or loud tests
Expand Down Expand Up @@ -215,7 +215,7 @@ sub options_ok { shift->_request_ok(options => @_) }

sub or {
my ($self, $cb) = @_;
$self->$cb unless $self->{latest};
$self->$cb unless $self->success;
return $self;
}

Expand Down Expand Up @@ -339,8 +339,7 @@ sub _request_ok {
sub _test {
my ($self, $name, @args) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 2;
$self->{latest} = Test::More->can($name)->(@args);
return $self;
return $self->success(!!Test::More->can($name)->(@args));
}

sub _text {
Expand Down Expand Up @@ -410,6 +409,13 @@ Current WebSocket message.
->json_message_hasnt('/bar')
->json_message_is('/foo/baz' => {yada => [1, 2, 3]});
=head2 success
my $bool = $t->success;
$t = $t->success($bool);
True if the last test was successful.
=head2 tx
my $tx = $t->tx;
Expand Down Expand Up @@ -744,7 +750,7 @@ arguments as L<Mojo::UserAgent/"options">, except for the callback.
$t = $t->or(sub {...});
Invoke callback if previous test failed.
Invoke callback if the value of L</"success"> is false.
# Diagnostics
$t->get_ok('/bad')->or(sub { diag 'Must have been Glen!' })
Expand Down
12 changes: 6 additions & 6 deletions t/mojolicious/testing_app.t
Expand Up @@ -13,17 +13,17 @@ use lib "$FindBin::Bin/lib";

use Test::Mojo;

my $t = Test::Mojo->new('MojoliciousTest');
my $mode = '';
$t->or(sub { $mode .= $t->app->mode })->or(sub { $mode .= shift->app->mode });
is $mode, 'testingtesting', 'both callbacks have been invoked';
my $t = Test::Mojo->new('MojoliciousTest');
my $success = '';
$t->or(sub { $success .= 'one' })->success(1)->or(sub { $success .= 'two' })
->success(!1)->or(sub { $success .= shift->app->mode });
is $success, 'onetesting', 'two callbacks have been invoked';
ok $t->get_ok('/')->success, 'test was successful';

# SyntaxError::foo in testing mode (syntax error in controller)
$t->get_ok('/syntax_error/foo')->status_is(500)
->or(sub { $mode .= $t->app->mode })
->header_is(Server => 'Mojolicious (Perl)')
->content_like(qr/Testing Missing/);
is $mode, 'testingtesting', 'callback has not been invoked';

# Foo::syntaxerror in testing mode (syntax error in template)
$t->get_ok('/foo/syntaxerror')->status_is(500)
Expand Down

0 comments on commit 2782efc

Please sign in to comment.