FAQ

On Tue, Dec 28, 2010 at 8:32 PM, James Russo wrote:

# create
ok($response = request(
POST('/appointment','content-type' =>
'application/json', 'content' => to_json($foo)
),
"create new appointment"));

Just wanted to put it out there in the archives for the next person.
Again, my suggestion is you put that into a test class -- you probably
don't want to type that dozens (if not hundreds) of times. And if your
application grows you will likely want some kind of custom test framework
that all your test files use. And If you work with other developers it's
helpful to have a simple test framework that's easy for everyone to use and
understand.

I use a generic test class that provides methods for making different types
of request and decoding responses, provides a $schema object (as you need to
modify data during a test), building and tearing down test data, and
provides access to the app configuration. I can enable a debug option to
dump full request and response and the class allows single-stepping through
the tests. And with your REST-like methods you probably want to know more
than than $response->is_success (which is what post_ok() checks). I then
sub-class that for each application so I can add specific helper methods
unique to that app.

So, tests end up something like:

my $test = Test::MyApp->new( debug_response => 0, dbic_trace => 0 );

my $user = $test->create_and_login_test_user;

$test->json_request( 201, POST => $path, \%data ); # OK if status == 201,
and print message about request.

my $response = $test->request( 200, GET => $path ); # $response is
HTTP::Response sub-class.

my $data = $response->decoded_content;
$data->{foo} = 'bar'; # modify response data before PUT

$test->json_request( 204, PUT => $path, $data );


The *request methods allow passing an HTTP::Request object, too, so it's
easy to customize a request for the rare times I need a custom header.



--
Bill Moseley
moseley@hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20101229/a114e3d4/attachment.htm

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 7 of 8 | next ›
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedDec 29, '10 at 12:47a
activeJan 1, '11 at 4:16p
posts8
users4
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase