Test::Mojo - json_has always pass #372

Closed
piotr-piatkowski opened this Issue Aug 16, 2012 · 2 comments

Comments

Projects
None yet
2 participants
@piotr-piatkowski

Here's simple test case:

#!/usr/bin/perl

package Test::App;

use Mojo::Base 'Mojolicious';
sub startup {
    shift->routes->get(
        '/test' => sub { shift->render( json => { x => 5 } ) } 
    );
}

package main;
use Test::More tests => 3;
use Test::Mojo;

my $t = Test::Mojo->new( 'Test::App' );
$t->get_ok( '/test' )
    ->json_has( '/x' ) # that's OK
    ->json_has( '/y' ) # but this should fail
    ;

Actually both json_has passes, second with empty description. This is due this line in Test/Mojo.pm:

sub json_has {                                                                                      
  my ($self, $p, $desc) = @_;                                                                       
  $desc ||= qq{has value for JSON Pointer "$p"};                                                    
  return $self->_test('ok',                                                                         
    Mojo::JSON::Pointer->new->contains($self->tx->res->json, $p), $desc);                           
}                                                                                                   

Here Pointer->new->contains is called in list context, so if test fails then it returns empty list, so $desc is taken as a test result and description is missing. All you have to do is to force scalar context - for example with double "!" or "scalar" operator.

What's funny, json_hasnt works fine as it uses "!" operator - which forces scalar context.

@kraih kraih closed this in 8ba8767 Aug 16, 2012

@kraih

This comment has been minimized.

Show comment Hide comment
@kraih

kraih Aug 16, 2012

Owner

Thanks, fixed.

Owner

kraih commented Aug 16, 2012

Thanks, fixed.

@piotr-piatkowski

This comment has been minimized.

Show comment Hide comment
@piotr-piatkowski

piotr-piatkowski Aug 16, 2012

Eight minutes. Impressive :-)

Eight minutes. Impressive :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment