Dancer::Test - Test helpers to test a Dancer application
use strict; use warnings; use Test::More tests => 2; use MyWebApp; use Dancer::Test; response_status_is [GET => '/'], 200, "GET / is found"; response_content_like [GET => '/'], qr/hello, world/, "content looks good for /";
This module provides test helpers for testing Dancer apps.
Be careful, the module loading order in the example above is very important. Make sure to use Dancer::Test after importing the application package otherwise your appdir will be automatically set to lib and your test script won't be able to find views, conffiles and other application content.
Dancer::Test
lib
For all test methods, the first argument can be either an array ref of the method and route, or a scalar containing the route (in which case the method is assumed to be GET), or a Dancer::Response object.
GET
# all 3 are equivalent response_status_is [ GET => '/' ], 200, 'GET / status is ok'; response_status_is '/', 200, 'GET / status is ok'; my $resp = dancer_response GET => '/'; response_status_is $resp => 200, 'GET / status is ok';
Asserts that the given request matches a route handler in Dancer's registry.
route_exists [GET => '/'], "GET / is handled";
Asserts that the given request does not match any route handler in Dancer's registry.
route_doesnt_exist [GET => '/bogus_path'], "GET /bogus_path is not handled";
Asserts that a response is found for the given request (note that even though a route for that path might not exist, a response can be found during request processing, because of filters).
response_exists [GET => '/path_that_gets_redirected_to_home'], "response found for unknown path";
Asserts that no response is found when processing the given request.
response_doesnt_exist [GET => '/unknown_path'], "response not found for unknown path";
Asserts that Dancer's response for the given request has a status equal to the one given.
response_status_is [GET => '/'], 200, "response for GET / is 200";
Asserts that the status of Dancer's response is not equal to the one given.
response_status_isnt [GET => '/'], 404, "response for GET / is not a 404";
Asserts that the response content is equal to the $expected string.
$expected
response_content_is [GET => '/'], "Hello, World", "got expected response content for GET /";
Asserts that the response content is not equal to the $not_expected string.
$not_expected
response_content_isnt [GET => '/'], "Hello, World", "got expected response content for GET /";
Similar to response_content_is(), except that if response content and $expected_struct are references, it does a deep comparison walking each data structure to see if they are equivalent.
If the two structures are different, it will display the place where they start differing.
response_content_is_deeply [GET => '/complex_struct'], { foo => 42, bar => 24}, "got expected response structure for GET /complex_struct";
Asserts that the response content for the given request matches the regexp given.
response_content_like [GET => '/'], qr/Hello, World/, "response content looks good for GET /";
Asserts that the response content for the given request does not match the regexp given.
response_content_unlike [GET => '/'], qr/Page not found/, "response content looks good for GET /";
Asserts that the response headers data structure equals the one given.
response_headers_are_deeply [GET => '/'], [ 'X-Powered-By' => 'Dancer 1.150' ];
Asserts that the response headers data structure includes some of the defined ones.
response_headers_include [GET => '/'], [ 'Content-Type' => 'text/plain' ];
Returns a Dancer::Response object for the given request.
Only $method and $path are required.
$params is a hashref, $body is a string and $headers can be an arrayref or a HTTP::Headers object, $files is an arrayref of hashref, containing some files to upload.
A good reason to use this function is for testing POST requests. Since POST requests may not be idempotent, it is necessary to capture the content and status in one shot. Calling the response_status_is and response_content_is functions in succession would make two requests, each of which could alter the state of the application and cause Schrodinger's cat to die.
my $response = dancer_response POST => '/widgets'; is $response->{status}, 202, "response for POST /widgets is 202"; is $response->{content}, "Widget #1 has been scheduled for creation", "response content looks good for first POST /widgets"; $response = dancer_response POST => '/widgets'; is $response->{status}, 202, "response for POST /widgets is 202"; is $response->{content}, "Widget #2 has been scheduled for creation", "response content looks good for second POST /widgets";
It's possible to test file uploads:
post '/upload' => sub { return upload('image')->content }; $response = dancer_reponse(POST => '/upload', {files => [{name => 'image', filename => '/path/to/image.jpg}]});
This method is DEPRECATED. Use dancer_response() instead.
my $logs = read_logs;
Returns an array ref of all log messages issued by the app since the last call to read_logs.
read_logs
For example:
warning "Danger! Warning!"; debug "I like pie."; is_deeply read_logs, [ { level => "warning", message => "Danger! Warning!" }, { level => "debug", message => "I like pie.", } ]; error "Put out the light."; is_deeply read_logs, [ { level => "error", message => "Put out the light." }, ];
See Dancer::Logger::Capture for more details.
This module is free software and is distributed under the same terms as Perl itself.
This module has been written by Alexis Sukrieh <sukria@sukria.net>
Test::More
To install Dancer, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dancer
CPAN shell
perl -MCPAN -e shell install Dancer
For more information on module installation, please visit the detailed CPAN module installation guide.