Test::WWW::Mechanize::Catalyst::WithContext - T::W::M::C can now give you $c
Version 0.03
use Test::WWW::Mechanize::Catalyst::WithContext; my $mech = Test::WWW::Mechanize::Catalyst::WithContext->new( catalyst_app => 'Catty' ); $mech->get_ok("/"); is $mech->ctx->stash->{foo}, "bar", "foo got set to bar"; $mech->post_ok("login", { u => "test", p => "secret" }); my $c = $mech->ctx; # $c is a Catalyst context is $c->session->{stuff}, "something", "things are in the session";
Test::WWW::Mechanize::Catalyst::WithContext is a subclass of Test::WWW::Mechanize::Catalyst that can give you the $c context object of the request you just did. This is useful for testing if things ended up in the stash correctly, if the session got filled without reaching into the persistence layer or to grab an instance of a model, view or controller to do tests on them. Since the cookie jar of your $mech will be used to fetch the context, things like being logged into your app will be taken into account.
$c
$mech
Besides that, it's just the same as Test::WWW::Mechanize::Catalyst. It inherits everything and does not overwrite any other functionality than the request making. See the docs of Test::WWW::Mechanize::Catalyst for more details.
Contains the context object for the most recent request. ctx and c are equivalent. Pick the one you feel more comfortable with.
ctx
c
is $mech->ctx->stash->{foo}, "bar", "foo got set to bar"; is $mech->c->stash->{foo}, "bar", "foo got set to bar"; # equivalent
If you need to keep an old context around to compare things before and after a request, assign it to a variable. The below example is contrived, but illustrates the idea.
$mech->get_ok("/cart"); my $first_c = $mech->ctx; $mech->get_ok("/cart/add/some_product"); isnt $first_c->cart->sum_total, $mech->ctx->cart->sum_total, "total cart value changed";
If you call this before you have made any requests, it will return undef. It will also return undef if there was no Catalyst context, which is the case if the request was handled by the Plack layer directly.
undef
This method was DEPRECATED! in version 0.03 and will likely be removed in a future version. Use $mech->ctx instead.
$mech->ctx
Does a GET request on $url and returns the HTTP::Response and the request context $c.
$url
my ( $res, $c ) = $mech->get_context('/');
This is not a get_ok and does not create test output.
get_ok
The following section gives a few examples where it's useful to have $c.
If the content that comes out of your application does not really contain any distinct markers it's very hard to check if the right stuff got rendered. Instead of trying to find something in your HTML that helps you identify the right page with one of the content checking methods like content_like, you can just look at the template name in the stash. Of course that doesn't tell you if it got rendered successfully, but it does tell you which template the controller decided should be rendered.
content_like
$mech->get_ok('/hard/to/verify/page); is $mech->ctx->stash->{template}, 'hard_to_verify.tt2', 'the right template got selected';
If you want to look at values in the session before and after some action, you would typically go and connect to the session store and peek around. For that, you need to know the type of the store, how to connect to it, and your current test user's session id. This is relatively trivial if a database (e.g. with Test::DBIx::Class), but gets more complicated when you're not mocking the store and it's something a little more esoteric. Of course you could use a different store for your unit tests, but maybe you don't want to do that.
Enter Test::WWW::Mechanize::Catalyst::WithContext. Just grab the context before and after you perform your action and look at the sessions.
$mech->get('/'); # or some other url my $c_before = $mech->ctx; $mech->get('/change/session'); my $c_after = $mech->ctx; isnt $c_before->session->{foo}, $c_after->session->{foo}, 'foo got changed';
Of course this could be arbitrarily complex.
If you find any bugs please open an issue on github.
Test::WWW::Mechanize::Catalyst
Test::WWW::Mechanize
WWW::Mechanize
Catalyst::Test
Catalyst
This module borrows parts of its test suite from Test::WWW::Mechanize::Catalyst.
simbabque <simbabque@cpan.org>
Robert Rothenberg
Copyright (C) simbabque.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Test::WWW::Mechanize::Catalyst::WithContext, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::WWW::Mechanize::Catalyst::WithContext
CPAN shell
perl -MCPAN -e shell install Test::WWW::Mechanize::Catalyst::WithContext
For more information on module installation, please visit the detailed CPAN module installation guide.