Skip to content

Commit 0c5595f

Browse files
committed
fix the test...
... it was expecting `user` to be a query parameter, whereas the example has it as a route param.
1 parent 4373240 commit 0c5595f

File tree

1 file changed

+71
-34
lines changed

1 file changed

+71
-34
lines changed

lib/Dancer2/Manual/Testing.pod

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,52 +52,89 @@ see the different options for sending parameters.
5252
=head3 Subtests
5353

5454
Tests can be separated using L<Test::More>'s C<subtest> functionality,
55-
thus creating multiple self-contained tests that don't overwrite each other.
55+
thus creating multiple self-contained tests that don't affect each other.
5656

57-
Assuming we have a different app that has two states we want to test:
57+
Assuming we have a different app that we want to test:
5858

59-
# MyApp.pm
60-
package MyApp;
61-
use Dancer2;
62-
set serializer => 'JSON';
6359

64-
get '/:user' => sub {
65-
my $user = route_parameters->get('user');
60+
# MyApp.pm
61+
package MyApp;
62+
use Dancer2;
63+
set serializer => 'JSON';
6664

67-
$user and return { user => $user };
65+
my %users = (
66+
jason => {
67+
name => 'Jason',
68+
likes => 'planes',
69+
},
70+
yanick => {
71+
name => 'Yanick',
72+
likes => 'orchids',
73+
}
74+
);
6875

69-
return {};
70-
};
76+
get '/:user' => sub {
77+
my $user = route_parameters->get('user');
7178

72-
1;
79+
my $user_data = $users{$user};
7380

74-
This is a contrived example of a route that checks for a user
75-
parameter. If it exists, it returns it in a hash with the key
76-
'user'. If not, it returns an empty hash
81+
if( $user_data ) {
82+
return { user => $user_data };
83+
}
7784

78-
# param.t
79-
use strict;
80-
use warnings;
81-
use Test::More;
82-
use Plack::Test;
83-
use HTTP::Request::Common;
84-
use MyApp;
85+
status 404;
8586

86-
my $test = Plack::Test->create( MyApp->to_app );
87+
return {
88+
error => 1,
89+
message => 'user not found'
90+
}
91+
};
8792

88-
subtest 'A empty request' => sub {
89-
my $res = $test->request( GET '/' );
90-
ok( $res->is_success, 'Successful request' );
91-
is( $res->content '{}', 'Empty response back' );
92-
};
93+
1;
9394

94-
subtest 'Request with user' => sub {
95-
my $res = $test->request( GET '/?user=sawyer_x' );
96-
ok( $res->is_success, 'Successful request' );
97-
is( $res->content '{"user":"sawyer_x"}', 'Empty response back' );
98-
};
95+
This is an example of tests for that route ensuring
96+
that we have the correct behavior with regard to the user
97+
parameter.
9998

100-
done_testing();
99+
# param.t
100+
use strict;
101+
use warnings;
102+
103+
use Test::More;
104+
use Plack::Test;
105+
use HTTP::Request::Common;
106+
use JSON qw/ decode_json /;
107+
108+
use MyApp;
109+
110+
# swap 'null' for 'note' to see the logs in your TAP output
111+
MyApp::set( logger => 'null' );
112+
113+
my $test = Plack::Test->create( MyApp->to_app );
114+
115+
subtest 'An empty request' => sub {
116+
my $res = $test->request( GET '/' );
117+
is $res->code => 404, 'user not provided, so not found';
118+
};
119+
120+
subtest 'Request with invalid user' => sub {
121+
my $res = $test->request( GET '/sawyer_x' );
122+
123+
ok !$res->is_success, 'user not found';
124+
is $res->code => 404, 'ressource not found';
125+
126+
is decode_json($res->content)->{message}, 'user not found', 'error message';
127+
};
128+
129+
subtest 'Request with valid user' => sub {
130+
my $res = $test->request( GET '/jason' );
131+
132+
ok $res->is_success, 'user found';
133+
134+
is decode_json($res->content)->{user}{likes}, 'planes', 'data present';
135+
};
136+
137+
done_testing();
101138

102139
=head1 Cookies
103140

0 commit comments

Comments
 (0)