Re: [Catalyst] Unit Testing
On Mon, Apr 21, 2008 at 06:01:17PM +1000, Yao Wang wrote: > Hi Everyone, > > After some research and tries. The testing code looks like following: > > BEGIN { use_ok 'MyApp::Controller::abc' }; > > my $c = MyApp->prepare(); No. Still not safe. Still not right. Don't. Stoppit. -- Matt S Trout Need help with your Catalyst or DBIx::Class project? Technical Directorhttp://www.shadowcat.co.uk/catalyst/ Shadowcat Systems Ltd. Want a managed development or deployment platform? http://chainsawblues.vox.com/http://www.shadowcat.co.uk/servers/ ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
[Catalyst] Unit Testing
Hi Everyone, After some research and tries. The testing code looks like following: BEGIN { use_ok 'MyApp::Controller::abc' }; my $c = MyApp->prepare(); $c = Test::MockObject::Extends->new($c); $c->set_always('forward',{}); MyApp::Controller::abc->get_info( $c); ok($c->stash->{template} eq "get_info.tt2", "comparing template returned for get_ info page"); What i tried is to check stash is correct and allow forward. What do you think about this ? Thanks, 61966 ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Unit Testing
From: "Kieren Diment" <[EMAIL PROTECTED]> Yeah, there's an accessability issue there. Anyway: perl -e 'for $i (1 .. 48) { system "lynx -dump -nolist http:// www.jrock.us/fp2008/catalyst/slide$i.html"}' > jrock.txt Thank you for those pages. I've also found and downloaded the source code. It already started to make me understand a little how Chained works, and maybe it will also make me start using Moose. Octavian ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Unit Testing
On Sun, Apr 20, 2008 at 12:51:44PM +1000, Yao Wang wrote: > Hi Everybody, > > Recently, i am looking for some modules for unit testing. I have seen > Catalyst::Test, Test::WWW::Mechanize::Catalyst etc, i think they are the > testing tools for application, not for unit testing. When i do the unit > testing, i need to pass $c and some other params to the methods right ? But > how can i create $c for testing ? > So can anybody tell me how to create this kind of $c ? Test::MockObject if you really want to. Personally I think that if you've got enough logic in your controller (as opposed to your model) to -need- unit testing you're doing something wrong. -- Matt S Trout Need help with your Catalyst or DBIx::Class project? Technical Directorhttp://www.shadowcat.co.uk/catalyst/ Shadowcat Systems Ltd. Want a managed development or deployment platform? http://chainsawblues.vox.com/http://www.shadowcat.co.uk/servers/ ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Unit Testing
On Sun, Apr 20, 2008 at 10:38:46AM +0300, Octavian Rasnita wrote: > From: "Dave Rolsky" <[EMAIL PROTECTED]> > >On Sat, 19 Apr 2008, John Romkey wrote: > > > >>I suspect that most Catalyst users build their applications so that the > >>controllers do too much work. I certainly did, and I'm gradually > >>rewriting mine to move most of the work into the model. Where before my > >>controllers > > > >A good way to approach a webapp is to think of the Controller as a thin > >shim between the the web/HTTP environment and your model. > >Of course, the ultimate benefit is that it's the only sane way to write an > >application ;) > > I heard this for more times, but I am usually creating my controllers using > a helper tool like: > > perl scripts/myapp_create.pl model MyData DBIC::Schema MyData create=static > dbi:mysql:database=mydata user pass > > Is this tool not recommended? Or should it be used only as a tool for > starting creating the model and the DBIC classes? That's a -scaffold- tool. So you use that to create an initial skeleton model, and then add your business logic methods below the md5 line so the skeleton parts can be regenerated. You should always be adding more code to your DBIC schema than you're adding to your controllers; if you find it's the other way round, you're doing it wrong. -- Matt S Trout Need help with your Catalyst or DBIx::Class project? Technical Directorhttp://www.shadowcat.co.uk/catalyst/ Shadowcat Systems Ltd. Want a managed development or deployment platform? http://chainsawblues.vox.com/http://www.shadowcat.co.uk/servers/ ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Unit Testing
On 20 Apr 2008, at 21:26, Octavian Rasnita wrote: From: "Jonathan Rockway" <[EMAIL PROTECTED]> * On Sun, Apr 20 2008, Octavian Rasnita wrote: Please tell us if we can find an example of putting the logic in the model than call those methods from the controller. http://www.jrock.us/fp2008/catalyst/start.html Using a DBIC-based model works the same way. Thank you Jonathan but I couldn't access any kind of helpful text on that page. I've look in the source code what link I need to access because it was hidden on the page. I accessed it, but I couldn't find a text in the following page neither. Please tell me at least if it is a text what I need to search for, or an image slide show (which unfortunately I can't see). Yeah, there's an accessability issue there. Anyway: perl -e 'for $i (1 .. 48) { system "lynx -dump -nolist http:// www.jrock.us/fp2008/catalyst/slide$i.html"}' > jrock.txt gives me: Catalyst Introducing Catalyst #1 Meta * I'm Jonathan Rockway * http://jrock.us/ * Exciting blog at http://blog.jrock.us/ Code and slides at http://jrock.us/ < < Previous | Index | Next >> "" Catalyst Introducing Catalyst #2 [1847190952.jpg] Catalyst Book * Hands-on tutorial * Learn DBIx::Class, Template Toolkit, and Catalyst http://www.packtpub.com/catalyst-perl-web-application/book < < Previous | Index | Next >> "" Catalyst Introducing Catalyst #3 What is Catalyst? * Web application environment * You write the code, Catalyst figures out how to run it * Development Server * FastCGI * mod_perl Niceties * Debugging pages * Plugins * CPAN-able components MVC framework * Separate concerns < < Previous | Index | Next >> "" Catalyst Introducing Catalyst #4 Hello < < Previous | Index | Next >> "" Catalyst Introducing Catalyst #5 Demo the wiki * Get the code now: http://jrock.us/KitiWiki-0.01.tar.gz * Code on slides abridged for clarity; tarball has everything. * Download now and follow along! Untar, perl Makefile.PL, make installdeps < < Previous | Index | Next >> "" Not Found The requested URL /fp2008/catalyst/slide6.html was not found on this server. _ Apache/2.2.8 (Debian) mod_perl/2.0.3 Perl/v5.8.8 Server at www.jrock.us Port 80 Not Found The requested URL /fp2008/catalyst/slide7.html was not found on this server. _ Apache/2.2.8 (Debian) mod_perl/2.0.3 Perl/v5.8.8 Server at www.jrock.us Port 80 Not Found The requested URL /fp2008/catalyst/slide8.html was not found on this server. _ Apache/2.2.8 (Debian) mod_perl/2.0.3 Perl/v5.8.8 Server at www.jrock.us Port 80 Not Found The requested URL /fp2008/catalyst/slide9.html was not found on this server. _ Apache/2.2.8 (Debian) mod_perl/2.0.3 Perl/v5.8.8 Server at www.jrock.us Port 80 Catalyst Introducing Catalyst #10 Getting started * catalyst.pl KitiWiki * create directories for our application < < Previous | Index | Next >> "" Catalyst Introducing Catalyst #11 Writing the Wiki Class * In KitiWiki::Backend::Wiki (lib/KitiWiki/Backend/Wiki.pm) package KitiWiki::Backend::Wiki;use Moose; has 'storage_root' => (is => 'ro', isa => Dir, ...); sub page {my ($self, $page) = @_;my $pagedir = $self- >storage_root->subdir($page);$self->_validate_page_name($page) or confess "attempt to load invalid page '$page'";return KitiWiki::Backend::Wiki::Page->new( storage => $pagedir );} < < Previous | Index | Next >> "" Catalyst Introducing Catalyst #12 Page Class * A bunch of functions, here's one: sub new_revision {my ($self, $author, $text) = @_;my $date = time;my $name = "$date-$author"; my $encoded = Encode::encode('utf8', $text); write_file($self->storage->file($name)->stringify, $encoded) or confess "Failed to write $name"; $self->_unshift_revision($name); return KitiWiki::Backend::Wiki::Page::Revision->new (storage => $self->storage,filename => $name, author => $author,date => $date,);} < < Previous | Index | Next >> "" Catalyst Introducing Catalyst #13 More Page code has 'revisions' => (metaclass => 'Collection::Array', is => 'ro',
Re: [Catalyst] Unit Testing
This is a great tutorial - many thanks. It also introduces Moose as a Object/Class framework which for some reason I hadn't come across before - at least until I stumbled across a recent conversation on the IRC channel. While I'm sure I would have seen this soon enough, I'm very glad I saw this before going too far down the line of using Class::Std in my code outside of Catalyst (perhaps wrongly assuming that PBP were actually PBP?). Cheers, Ian On Sun, Apr 20, 2008 at 10:32 AM, Jonathan Rockway <[EMAIL PROTECTED]> wrote: > * On Sun, Apr 20 2008, Octavian Rasnita wrote: > > > Please tell us if we can find an example of putting the logic in the > > model than call those methods from the controller. > > http://www.jrock.us/fp2008/catalyst/start.html > > Using a DBIC-based model works the same way. > > Regards, > Jonathan Rockway > > -- > print just => another => perl => hacker => if $,=$" > > ___ > List: Catalyst@lists.scsys.co.uk > Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst > Searchable archive: > http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ > Dev site: http://dev.catalyst.perl.org/ > ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Unit Testing
From: "Jonathan Rockway" <[EMAIL PROTECTED]> * On Sun, Apr 20 2008, Octavian Rasnita wrote: Please tell us if we can find an example of putting the logic in the model than call those methods from the controller. http://www.jrock.us/fp2008/catalyst/start.html Using a DBIC-based model works the same way. Thank you Jonathan but I couldn't access any kind of helpful text on that page. I've look in the source code what link I need to access because it was hidden on the page. I accessed it, but I couldn't find a text in the following page neither. Please tell me at least if it is a text what I need to search for, or an image slide show (which unfortunately I can't see). Thank you. Octavian ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Unit Testing
* On Sun, Apr 20 2008, Octavian Rasnita wrote: > Please tell us if we can find an example of putting the logic in the > model than call those methods from the controller. http://www.jrock.us/fp2008/catalyst/start.html Using a DBIC-based model works the same way. Regards, Jonathan Rockway -- print just => another => perl => hacker => if $,=$" ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Unit Testing
From: "Dave Rolsky" <[EMAIL PROTECTED]> On Sat, 19 Apr 2008, John Romkey wrote: I suspect that most Catalyst users build their applications so that the controllers do too much work. I certainly did, and I'm gradually rewriting mine to move most of the work into the model. Where before my controllers A good way to approach a webapp is to think of the Controller as a thin shim between the the web/HTTP environment and your model. Of course, the ultimate benefit is that it's the only sane way to write an application ;) I heard this for more times, but I am usually creating my controllers using a helper tool like: perl scripts/myapp_create.pl model MyData DBIC::Schema MyData create=static dbi:mysql:database=mydata user pass Is this tool not recommended? Or should it be used only as a tool for starting creating the model and the DBIC classes? Please tell us if we can find an example of putting the logic in the model than call those methods from the controller. Thanks. Octavian ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Unit Testing
On Sat, 19 Apr 2008, John Romkey wrote: I suspect that most Catalyst users build their applications so that the controllers do too much work. I certainly did, and I'm gradually rewriting mine to move most of the work into the model. Where before my controllers A good way to approach a webapp is to think of the Controller as a thin shim between the the web/HTTP environment and your model. The controller code should handle web/HTTP-specific things like URIs, form submissions, etc. and translate that into API calls on model classes. It also might handle something like authentication, since the way you authenticate a user via the web is different than how you'd do it via the command line. Besides the testability benefits, another benefit is that it makes it easy to reuse this business logic in things like cron jobs, daemons, and command line driven code. Invariably, any app of a certain size will need this flexibility. Of course, the ultimate benefit is that it's the only sane way to write an application ;) -dave /*== VegGuide.Org Your guide to all that's veg ==*/ ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Unit Testing
On Apr 19, 2008, at 10:51 PM, Yao Wang wrote: Hi Everybody, Recently, i am looking for some modules for unit testing. I have seen Catalyst::Test, Test::WWW::Mechanize::Catalyst etc, i think they are the testing tools for application, not for unit testing. When i do the unit testing, i need to pass $c and some other params to the methods right ? But how can i create $c for testing ? So can anybody tell me how to create this kind of $c ? Yao, Here's what I'd suggest: Structure your application so that all the real work is done in a set of modules which Catalyst then calls to do the work. Use Catalyst as a wrapper to interface your application to the web. Catalyst documents refer to this as the "business logic" of your application. The application itself will have no dependencies on Catalyst and can be tested independently of it. The application wrapped in Catalyst can be tested using the testing tools you mentioned above. I suspect that most Catalyst users build their applications so that the controllers do too much work. I certainly did, and I'm gradually rewriting mine to move most of the work into the model. Where before my controllers would interact directly with the database, now they ask the model to do it for them. The model is now the heart of the application; it does all the work and Catalyst only glues it to the web. You could discard Catalyst and build a command-line interface using the same model, or build some other kind of interface and not have to change the model at all. Suppose you need to be able to create a user account for your web site. You probably want to do a variety of tests on the account information... does an account already exist with that email address? Is the password strong or weak? Does the account name use any prohibited words in it? And if the account is acceptable you need to write at least one record, possibly more, into a database. The way I originally wrote my controller, the controller itself did all that itself. The way I've rewritten it, my business logic model does it for me. The controller has an action in it which handles the account creation request. That action gathers the necessary information from the request, performs any web-related validation on it (at least making sure all the needed parameters are there) and then calls the model to create the account. If there's an error, it reports the error back to the user. Now if you want a command-line utility to create an account, you just write a Perl script which gets its arguments from the command-line and calls the same business logic. Then you can write unit tests that completely bypass Catalyst and call the business logic to confirm that it properly creates an account, or properly rejects an account with a bad password or unacceptable word in its name. And you can write Catalyst tests to confirm that Catalyst correctly maps web actions into the business logic. This is all a bit more work than might seem necessary at first, but if your application evolves at all you'll probably find that modularizing it in this manner really helps make it more understandable and maintainable in the long term, as well as making it more testable. - john romkey http://www.romkey.com/ ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Unit Testing
HI Yao, I solved this by reverse-engineering the context with a debug session ($DB::single=1). In short, $c is an instance of MyApp. You may have to manually instantiate some of the internal hashes, etc for the needs of the test target. I'll assume that if you're truly unit-testing, you'll be mocking the other externals of your controller. Hope that helps. -- Peter Fitzgibbons -- iPhone -- "IT"-ness. href= http://www.macdailynews.com/index.php/weblog/comments/apples_iphone_could_become_iconic_it_object -- ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
[Catalyst] Unit Testing
Hi Everybody, Recently, i am looking for some modules for unit testing. I have seen Catalyst::Test, Test::WWW::Mechanize::Catalyst etc, i think they are the testing tools for application, not for unit testing. When i do the unit testing, i need to pass $c and some other params to the methods right ? But how can i create $c for testing ? So can anybody tell me how to create this kind of $c ? Thanks, Yao ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/