Best way to test using Test::WWW::Mechanize?
Hey, all, I'm starting a new app, and want to be able to build my tests concurrently. In prior projects I've used T::W::M::Catalyst, and want to use it to handle testing for my new app. I guess my first question is whether ::PSGI is the most appropriate flavor to use---it would appear to me to be, but I suppose I could have missed a ::Dancer flavor that hasn't been released or something? Anyway, proceeding on that basis, I have gotten it working, but I have a question about what I had to do to get it to work versus what I expected to work. So, this worked (using the output of 'dancer -a Foo'): ----->8----- #!/usr/bin/perl use Dancer qw{:syntax}; use Foo; use Test::More tests => 2; require Test::WWW::Mechanize::PSGI; set apphandler => 'PSGI'; my $test = Test::WWW::Mechanize::PSGI->new (app => do ("bin/app.pl")); $test->get_ok ('/') or diag $test->content; $test->content_contains ('Tutorial'); -----8<----- The thing that confuses and annoys me is the need to use 'do'---mostly because I don't quite understand why I need it. I tried: ----->8----- #!/usr/bin/perl use Dancer; use Foo; use Test::More tests => 2; require Test::WWW::Mechanize::PSGI; set apphandler => 'PSGI'; my $test = Test::WWW::Mechanize::PSGI->new (app => dance); $test->get_ok ('/') or diag $test->content; $test->content_contains ('Tutorial'); -----8<----- and it failed with a 404. Since that pretty much recapitulates the content of bin/app.pl, and the result of do ("bin/app.pl") should be the return value of dance, I'm a little confused. Any enlightenment? Regardless, other than this small roadbump, I'm rather enjoying Dancer as a development platform. Thanks for all your efforts. Mike.
On Tue, Feb 22, 2011 at 3:35 PM, Michael Alan Dorman <mdorman@ironicdesign.com> wrote:
So, this worked (using the output of 'dancer -a Foo'):
----->8----- #!/usr/bin/perl use Dancer qw{:syntax}; use Foo; use Test::More tests => 2; require Test::WWW::Mechanize::PSGI; set apphandler => 'PSGI'; my $test = Test::WWW::Mechanize::PSGI->new (app => do ("bin/app.pl")); $test->get_ok ('/') or diag $test->content; $test->content_contains ('Tutorial'); -----8<-----
The thing that confuses and annoys me is the need to use 'do'---mostly because I don't quite understand why I need it. I tried:
http://search.cpan.org/dist/Test-WWW-Mechanize-PSGI/lib/Test/WWW/Mechanize/P... needs the code ref to start your app, which is what you give it with the " do 'filename' " construct. The reason it needs that is due to what the PSGI specification says an app _is_: http://search.cpan.org/dist/PSGI/PSGI.pod#Applications Applications: Applications are web applications that actually get HTTP requests and return HTTP response. In PSGI it's a *code reference* http://search.cpan.org/dist/PSGI/PSGI.pod#SPECIFICATION A PSGI application is a Perl code reference. the do() you are using gives the PSGI tester the code reference it needs (your app) to run tests against. -marco- -- Marco Fontani Glasgow Perl Mongers - http://glasgow.pm.org/ Join the RackSpace Cloud at: http://www.rackspacecloud.com/277.html
Marco Fontani <mfontani@cpan.org> writes:
the do() you are using gives the PSGI tester the code reference it needs (your app) to run tests against.
Hi, Marco, thanks for your response. I do actually understand what PSGI wants, and I'm even in the failing case, I am providing it with a codref, otherwise I wouldn't be getting a 404 page generated by Dancer when I request '/'. My question is why is it that dance() is giving back an incorrectly initialized application object in my failing test script, when it gives back a correct one when it is insulated by a do "" or eval ""? Mike.
participants (2)
-
Marco Fontani -
Michael Alan Dorman